I’m trying to pass a collection to the view but I’m not getting anything.
The page renders blank
$companies = Companies::where('key',$key); return View('view.name', compact('companies'));
In the blade I have
@foreach($companies as #company) {{$company->anything}} @endforeach
The result is
Error: Trying to get property ‘anything’ of non-object
From what I found this should work, please let me know what I`m doing wrong
Thanks for your help
Advertisement
Answer
You forgot to add the ->get()
to get the collection.
If you don’t use ->get()
or ->first()
, your variable is still in ‘QueryBuilder’ format rather than ‘Collection’.
$companies = Companies::where('key',$key)->get(); return view('view.name', compact('companies'));
A tip: You can use dd()
in Laravel to be able to debug the variable.
For example: dd($companies);