How to pass variable in laravel View?

How to pass the variable I need to be available in every view?
Now it's over to only using controllers each view function separately, and I'm over all that was visible.
return view('home')->with('role',$role);
March 23rd 20 at 19:41
2 answers
March 23rd 20 at 19:43
View Composers
AppServiceProvider
But judging by the name of the variable, nothing in the templates do not need to transfer, and to use the Directive can
Or he just wants to get the role name as much as he can. And Yes, the composers here redundant. - jesse_Reiche commented on March 23rd 20 at 19:46
+ - Queen_Heller commented on March 23rd 20 at 19:49
ViewComposers the most convenient solution. Why is it redundant? @jesse_Reiche - Queen_Heller commented on March 23rd 20 at 19:52
@Queen_Heller, he means it
View::share('role',$role);
The author of this option for you - newell_Emard commented on March 23rd 20 at 19:55
@newell_Emard, and you offer for all the code spread View::share? Then how to search?
and if God forbid, modular architecture? Let's say you added a migration with a new field in the model, will all calls to View::share correct?

You can do anything in my project, but don't embarrass others to indecent acts - Queen_Heller commented on March 23rd 20 at 19:58
@Queen_Heller, it is done once at boot appserviceprovider. The link in the answer
@Rubie_Kutch87 the author why are you so quiet? We are here for someone trying - newell_Emard commented on March 23rd 20 at 20:01
@newell_Emard, in General I Controller-e made a method that returns this variable and pass it in every view before creating. - Rubie_Kutch87 commented on March 23rd 20 at 20:04
@Rubie_Kutch87, you AppServiceProvider there yet? - newell_Emard commented on March 23rd 20 at 20:07
March 23rd 20 at 19:45
You can make a global array, for example
$view_data = [];

$this->view_data['var_1'] = $data;

return view('view_file', $this->view_data);

Everywhere to use this design and the output to an array with data and process them in the view
Also the documentation not read? - jesse_Reiche commented on March 23rd 20 at 19:48
@jesse_Reiche, but not read only after my post =)
Lara opened a very long time - jalyn_Gerla commented on March 23rd 20 at 19:51
@jalyn_Gerla, and for what, then, is the offer just not know anything? On Yii must write according to the style - gail_Kling41 commented on March 23rd 20 at 19:54

Find more questions by tags Laravel