how to compact this for?

  • Last Update :
  • Techknowledgy :

The View::make function takes 3 arguments which according to the documentation are:

public View make(string $view, array $data = array(), array $mergeData = array())

On the other hand, you can use with() as many time as you like. Thus, this will work:

return View::make('gameworlds.mygame')

   > with(compact('fixtures'))

   > with(compact('teams'))

   > with(compact('selections'));

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']));

I was able to use

return View::make('myviewfolder.myview', compact('view1', 'view2', 'view3'));

viewblade is the frontend (view) blade.

return view('viewblade', compact('view1', 'view2', 'view3', 'view4'));
Route::get('/', function () {
    return view('greeting', ['name' => 'James']);
        <h1>Hello, {{ $name }}</h1>


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'));

You can pass array of variables to the compact as an arguement eg:

return view('yourView', compact(['var1', 'var2', ....

in view: if var1 is an object you can use it something like this

@foreach($var1 as $singleVar1) {
      $singleVar1 - > property

incase of scalar variable you can simply


Suggestion : 2

This shot explains how to use the compact() method to elegantly parse data to your views.,When you have several variables with saved data and you want to parse to view, you can use the compact() method.,The compact() method makes it a lot easier to parse data to the view in a readable format.,In the code above, we return the comment view, which should typically be in your resources/views directory as comment.blade.php. We parse the data as a string, in the same fashion as it is written.

For example:

users = User::all();$
posts = Post::all();
return view('/comment', compact('users', 'posts'));

Suggestion : 3

Last Updated : 29 Nov, 2019


array compact("variable 1", "variable 2"...)


Input: $AS = "ASSAM", $OR = "ORISSA", $KR = "KERELA"
compact("AS", "OR", "KR");
      [AS] => ASSAM[OR] => ORISSA[KR] => KERELA


      [AS] => ASSAM[OR] => ORISSA[KR] => KERELA