Good day to all gentlemen. Developing a website on Laravel. There are registered and unregistered users. For the first, some features are added in the form of links in the header of the site. The whole cap is included as follows.

@extends("layouts/header_footer") @section("header") @parent @endsection @section("footer") @parent @endsection 

How do I pass in the header variable $ user. In the template itself, checking the session is not a hunt and not according to mvc-shovski. Or who has any options for making the caps in a separate file. Thank you all for your help and ideas.

  • What's the problem? Take out everything you need in a separate file and connect via @include. Or is the question of globally passing a variable? - Vladimir Gonchar pm
  • @VladimirGonchar You are absolutely right, you need to globally pass a variable. From the controller, I transfer it to the view that pulls up the cap. Here I also need to transfer a variable to a cap. - Vladislav
  • Unless to split the header and footer into two different files and call through view-> render (); for template engine is powerless for now. - Vladislav

1 answer 1

If it is not critical that the variable can be displayed in all views, then you can create a service provider (the name can be any, took View).

 php artisan make:provider ViewServiceProvider 

And in the boot method:

 if(Auth::check()){ $user = User::where('id', '=', Auth::user()->getAuthIdentifier())->first(); view()->share('user', $user); } 

In the submission, respectively, check who you give to someone through:

 @auth Для авторизированных, например, $user->name @endauth Для мимо проходящих @guest @endguest 
  • Thank you very much, I will test it later. - Vladislav