I just started shifting to Laravel 5 from Symfony and I am wondering how to pass multiple arrays from my controller to my view.
I am trying to use PHP's compact() function but I am unable to properly get them in the view.
$users = Users::all();
$projects = Projects::all();
$foods = Foods::all();
return ('controller.view', compact('users','projects','foods'));
What is the best for me to be able to transfer all these array of objects to my view.
Any help would be greatly appreciated.
Thanks!
You're missing call to the view method in your example
return view('controller.view', compact('users','projects','foods'));
That said, the rest of the syntax is correct.
In your view you can access those variables as you would normally. If you're using blade for example.
In resources/views/controller/view.blade.php
@foreach ($users as $user)
{{$user->property}}
@endforeach
If you're not using blade.
In resources/views/controller/view.php
<?php
foreach ($users as $user) {
echo $user->property;
}
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With