型号:产品:
public function category() {
return $this->belongsTo('App\categories','categories_id');
}
路线:
Route::get('/showproducts',function () {
return view('admin.showproducts', [
'mydata' => App\products::with('category')->get()
]);
});
查看:显示产品:
<table> <!-- Added by edit -->
@foreach($mydata as $product)
<tbody>
<tr>
<th scope="col">{{$count++}}</th>
<td>{{$product->pro_name}}</td>
<td>{{$product->pro_code}}</td>
<td>{{$product->price}}</td>
<td>{{$product->category->category_name}}</td>
<td ><img src="{{url('/myimages')}}/{{$product->pro_img}}" style="width:80px; height: 60px;"></td>
<td><a href="{{url('/editproduct')}}/{{$product->id}}" class="btn btn-fill btn-primary btn-xs">Edit</a> |
<a href="{{url('/')}}" class="btn btn-fill btn-danger btn-xs">Delete</a>
</td>
</tr>
</tbody>
@endforeach
</table>
我想回显category_name
字段,但是它没有显示在视图上。在检索集合时,一切都可以。唯一的问题是要迭代
<td>{{$product->category->category_name}}</td>
我该怎么做?