Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5:Pass Multiple Array From Controller to View

Tags:

laravel-5

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!

like image 977
user3880082 Avatar asked Jun 18 '15 09:06

user3880082


1 Answers

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;
}
?>
like image 73
Ben Swinburne Avatar answered Sep 20 '22 13:09

Ben Swinburne