[英]Cant display value selected from database with inner join using yii framework
[英]Display value from the database with inner join on laravel 8
我正在嘗試顯示數據庫中的所有產品,在產品表中,我存儲了 colors、類型、產品類型、品牌的 4 個表的 ID
在我最初的項目中,我使用了:
"SELECT * FROM product
join brand
on product.brand_id = brand.id join product_type
on product.product_type_id = product_type.id join type
on product.type_id = type.id WHERE product.is_deleted = '0'";
現在我正在嘗試做同樣的事情,但在 Laravel 8 上,但我收到以下錯誤:
為 foreach() 提供的參數無效
在 App\Http\Controllers\ProductController.php 中(一開始我只使用了下面的代碼,但沒有加入並且它有效,我無法顯示 color_name 或這些表中的任何內容)
public function index()
{
return view('admin.product.product',[
'product' => Product::all()
->join('brand', 'products.brand_id', '=', 'brands.id')
]);
}
在product.blade.php(產品展示頁面)
@php
$nr = 1;
@endphp
@foreach ($product as $row_product)
<tr>
<td><p><?=$nr++?></p></td>
<td><p>{{ $row_product->product_name }}</p></td>
<td><p>{{ $row_product->brand_name }}</p></td>
<td><p>$ {{ $row_product->price }}</p></td>
<td><p>{{ $row_product->year }}</p></td>
<td><p>{{ $row_product->product_type_name }}</p></td>
<td><p>{{ $row_product->type_name }}</p></td>
<td><p>{{ $row_product->description }}</p></td>
<td><p>{{ $row_product->created_at }} ({{ $row_product->created_at->diffForHumans() }}) </p></td>
<td><p>Added By</p></td>
<td><p>{{ $row_product['updated_at'] }} ({{ $row_product->updated_at->diffForHumans() }})</p></td>
<td><p>Modified By</p></td>
<td>
<form action="/admin/product/{{ $row_product->product_id }}" method="POST" class="">
@csrf
@method('delete')
<a class="btn btn-outline-primary" href="{{ route('edit_product',$row_product->product_id) }}">Edit</a>
<button class="btn btn-outline-primary ms-0" type="submit" onclick="return confirm('Are you sure ?')">Delete</button>
{{-- <a class="btn btn-outline-primary mt-2" href="admin.php?page=product_size_color_list&id=<//?=$row['product_id']?>">Product variants</a> --}}
</form>
</td>
</tr>
我做錯了什么? 以及我現在如何展示具有內部連接原因的產品我沒有想法
您可以使用select將字段添加到結果中:
Product::all()->join('brand', 'products.brand_id', '=', 'brand.id')->select('products.*', 'brand.brand_name')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.