I have a database table with
id | name | url |
1 | Bike | bike|
2 | Auto | auto|
I want to fetch the all names from the table to view with foreach loop. But it returns
Trying to get property of non-object
Controller
public function category($url, request $id)
{
$cat = Category::where('url', $url)->firstorfail();
return view ('view', compact('category'));
}
Model
class Category extends Model
{
protected $fillable = ['name', 'url'];
}
View
@foreach($category as $cat)
{{ $cat->name }}
//{{ $cat['name] }} does not return anything.
@endforeach
//{{ $cat->name }} returns only one category name
I saw enter link description here, I couldn't solve it.
firstorfail
but in the view you are iterating ... either remove theforeach
or pass an array ofCategory
– ka_linfirstorfail
withget()
, still no success. If I tried$cat->all()
. ItsCall to a member function all() on boolean
. It is the issue? – universalget
is used when specifing awhere
clause with/or sorting. If you want to display 1 item remove theforeach
in the template. If you want multiple then pass to the view the array – ka_lin