How to pass a variable to a blade?

-1

I have the following scenario: Some types of users may have access to the site, say USERTYP1, USERTYP2 or USERTYP3.

The menu of my application changes according to the type of user.

I have a master template homeMaster.blade.php and the menu was created in the home.blade.php file that inherits the master template.

In traditional php should have if in home.blade.php to test a $usertype variable. According to the type we would have the appropriate menu.

I was reading the blade documentation and created this code to test the user:

@if ($usertype === "USERTYP1")
    menu 1
@elseif ($usertype === "USERTYP2")
    menu 2
@else
    menu 3
@endif

I did not understand how to pass this variable $usertype to home.blade.php , or even if this would be the best approach for this type of implementation.

-

Updating

I'll post here for anyone who needs it. I found a link that exactly answers my question.

    
asked by anonymous 20.07.2016 / 20:48

2 answers

1

Hello,

You can pass an array when you call the view, as the second, containing the variables you want to use in your view. Example:

view('home', ["usertype" => "USERTYP1"]);

If you want to pass the variable into an include call, you can follow the example:

@include("home", ["usertype" => "USERTYP1"])

I hope I have helped.

    
20.07.2016 / 21:21
1

You can also use the compact

ex:

public function edit($id) {

      $categorias = Categorias::all();
      $posts =  Posts::all();

       return view('edit')->with(compact('categorias','posts'));

}

and in your view you can capture this value normally

{{ $categorias }}
{{ $posts }}
    
21.07.2016 / 18:52