Laravel雄辩的收藏无法在view blade.php中循环

时间:2018-08-31 19:04:29

标签: laravel-5.6

型号:产品:

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>

我该怎么做?

0 个答案:

没有答案