从 laravel 上的 2 table 获取值
Get the value from 2 table on laravel
Schema::create('discounts', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->dateTime('from', $precision =0);
$table->dateTime('to', $precision =0);
$table->enum('status', [0, 1, 2])->default(0)->comment('0:active 1:expired 2:scheduled');
$table->timestamps();
$table->softDeletes();
});
Schema::create('discount_product', function (Blueprint $table) {
$table->id();
$table->string('code');
$table->unsignedBigInteger('discount_id');
$table->unsignedBigInteger('product_id');
$table->foreign('discount_id')->references('id')->on('discounts')->onDelete('cascade');
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->float('rate');
$table->timestamps();
$table->softDeletes();
});
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name')->unique();
$table->float('price');
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('category_id');
$table->unsignedBigInteger('brand_id');
$table->string('image')->nullable();
$table->text('description')->nullable();
$table->integer('quantity');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
$table->foreign('brand_id')->references('id')->on('brands')->onDelete('cascade');
$table->timestamps();
$table->softDeletes();
});
public function discountProducts()
{
return $this->hasMany(DiscountProduct::class);
}
public function discount()
{
return $this->belongsTo(Discount::class);
}
/**
* Get product of discount
*/
public function product()
{
return $this->hasMany(Product::class);
}
我有 3 个这样的表:折扣,discount_product,产品,在我的 detailProduct.blade.php 中,我想让产品有折扣,但我不知道该怎么做。有人能帮我吗 ?非常感谢
这是我的:查看详细信息功能:我得到 discount_products
public function show($id)
{
$discount = $this->discountRepository->getDiscountById($id);
$discount_products = $this->discountRepository->getDiscontProductOnDiscount($id);
return view('user.discounts.detailDiscount',
['discount_products' => $discount_products, 'discount' => $discount]);
}
你可以这样做
$data['discount'] = $this->discountRepository->getDiscountById($id);
$data['discount_products'] = $this->discountRepository->getDiscontProductOnDiscount($id);
return view('user.discounts.detailDiscount', $data);
并且在您看来,您可以访问这样的数据 $discount
、$discount_products
如果你的 $product 中有很多数据,你不能先 foreach 然后在视图中像这样打印,我假设你会在 table 打印所以会喜欢这样
<tbody>
@foreach ($products as $row)
<tr>
<td>{{$row->name}}</td>
<td>{{$row->price}}</td>
<tr>
@endforeach
</tbody>
如果您的数据只有 1 行,您可以像这样访问
$products[0]->name
Schema::create('discounts', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->dateTime('from', $precision =0);
$table->dateTime('to', $precision =0);
$table->enum('status', [0, 1, 2])->default(0)->comment('0:active 1:expired 2:scheduled');
$table->timestamps();
$table->softDeletes();
});
Schema::create('discount_product', function (Blueprint $table) {
$table->id();
$table->string('code');
$table->unsignedBigInteger('discount_id');
$table->unsignedBigInteger('product_id');
$table->foreign('discount_id')->references('id')->on('discounts')->onDelete('cascade');
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->float('rate');
$table->timestamps();
$table->softDeletes();
});
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name')->unique();
$table->float('price');
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('category_id');
$table->unsignedBigInteger('brand_id');
$table->string('image')->nullable();
$table->text('description')->nullable();
$table->integer('quantity');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
$table->foreign('brand_id')->references('id')->on('brands')->onDelete('cascade');
$table->timestamps();
$table->softDeletes();
});
public function discountProducts()
{
return $this->hasMany(DiscountProduct::class);
}
public function discount()
{
return $this->belongsTo(Discount::class);
}
/**
* Get product of discount
*/
public function product()
{
return $this->hasMany(Product::class);
}
我有 3 个这样的表:折扣,discount_product,产品,在我的 detailProduct.blade.php 中,我想让产品有折扣,但我不知道该怎么做。有人能帮我吗 ?非常感谢
这是我的:查看详细信息功能:我得到 discount_products
public function show($id)
{
$discount = $this->discountRepository->getDiscountById($id);
$discount_products = $this->discountRepository->getDiscontProductOnDiscount($id);
return view('user.discounts.detailDiscount',
['discount_products' => $discount_products, 'discount' => $discount]);
}
你可以这样做
$data['discount'] = $this->discountRepository->getDiscountById($id);
$data['discount_products'] = $this->discountRepository->getDiscontProductOnDiscount($id);
return view('user.discounts.detailDiscount', $data);
并且在您看来,您可以访问这样的数据 $discount
、$discount_products
如果你的 $product 中有很多数据,你不能先 foreach 然后在视图中像这样打印,我假设你会在 table 打印所以会喜欢这样
<tbody>
@foreach ($products as $row)
<tr>
<td>{{$row->name}}</td>
<td>{{$row->price}}</td>
<tr>
@endforeach
</tbody>
如果您的数据只有 1 行,您可以像这样访问
$products[0]->name