laravel compact() and ->with()
laravel compact() and ->with()
I have a piece of code and I'm trying to find out why one variation works and the other doesn't.
return View::make('gameworlds.mygame', compact('fixtures'), compact('teams'))->with('selections', $selections);
This allows me to generate a view of arrays for fixtures, teams and selections as expected.
However,
return View::make('gameworlds.mygame', compact('fixtures'), compact('teams'), compact('selections'));
does not allow the view to be generated properly. I can still echo out the arrays and I get the expected results but the view does not render once it arrives at the selections section.
It's oké, because I have it working with the ->with() syntax but just an odd one.
->with()
Thanks.
DS
6 Answers
6
The View::make function takes 3 arguments which according to the documentation are:
View::make
public View make(string $view, array $data = array(), array $mergeData = array())
In your case, the compact('selections') is a 4th argument. It doesn't pass to the view and laravel throws an exception.
compact('selections')
On the other hand, you can use with() as many time as you like. Thus, this will work:
with()
return View::make('gameworlds.mygame')
->with(compact('fixtures'))
->with(compact('teams'))
->with(compact('selections'));
Or
->with(compact('fixtures', 'teams', 'selections'))– JCarlos
May 31 '16 at 21:38
->with(compact('fixtures', 'teams', 'selections'))
I just wanted to hop in here and correct (suggest alternative) to the previous answer....
You can actually use compact in the same way, however a lot neater for example...
return View::make('gameworlds.mygame', compact(array('fixtures', 'teams', 'selections')));
Or if you are using PHP > 5.4
return View::make('gameworlds.mygame', compact(['fixtures', 'teams', 'selections']));
This is far neater, and still allows for readability when reviewing what the application does ;)
I was able to use
return View::make('myviewfolder.myview', compact('view1','view2','view3'));
I don't know if it's because I am using PHP 5.5 it works great :)
Route::get('/', function () {
return view('greeting', ['name' => 'James']);
});
<html>
<body>
<h1>Hello, {{ $name }}</h1>
</body>
</html>
or
public function index($id)
{
$category = Category::find($id);
$topics = $category->getTopicPaginator();
$message = Message::find(1);
// here I would just use "->with([$category, $topics, $message])"
return View::make('category.index')->with(compact('category', 'topics', 'message'));
}
the best way for me :
$data=[
'var1'=>'somthing',
'var1'=>'somthing',
'var1'=>'somthing',
];
return View::make('view',$data);
Laravel Framework 5.6.26
return more then one array then we use campact('array1','array2','array3', ....) to return view.
viewblade is the frontend (view) blade.
return view('veiwblade', compact('veiw1','veiw2','veiw3','view4'));
Please add an explaination to your code. Code only solutions are quite useless.
– L. Guthardt
Jul 2 at 11:13
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
thanks very much.
– dstewart101
Mar 14 '14 at 20:43