我有一个类别表,它们具有以下字段。
我想显示parent_id =(例如)= 2的类别,但是我看到此错误。
ErrorException(E_ERROR)
试图获取非对象的属性(视图:C:\ xampp \ htdocs \ new \ shopping \ resources \ views \ Home \ networks.blade.php) 以前的例外情况
试图获取非对象(0)的属性
NetworkController.php
public function networks()
{
$categories = Category::where('parent_id', 2)->first();
return view('Home.networks', compact('categories'));
}
networks.blade.php
@foreach($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
@endforeach
答案 0 :(得分:0)
$ categories = Category :: where('parent_id',2)-> first();
只需将其替换为
$categories = Category::where('parent_id', 2)->get();
作为参考,请检查此链接 the difference of find and get in Eloquent in Laravel