how to share data to all view in laravel 5.7

i have a login form. i want when user login success, my controller will select data of this user and share to all view.

I read laravel docs. This advises me to use the "View::share('key', 'value');" in boot method of AppServiceProvider, but i dont know how to call it in my controller

my controller:

if( Auth::attempt(['mssv' => $username,'password' =>$password])) {
                $user=SinhVien::where('mssv',$username)->first();

       //i want to share $user to all view in here

                $success = new MessageBag(['successlogin' => 'Login Success]);
                return redirect()->back()->withErrors($success);
            }
else {
            $errors = new MessageBag(['errorlogin' => 'Login Fail']);
            return redirect()->back()->withErrors($errors);
        }

and in all view blade, i use:

{{$user->name}}

Please help!

In your blades you would then have access to the key from the View::share('key', 'value'); you put in the AppServiceProvider.

So if the key is user, in your blades you just use the $user variable.

Views - Laravel, use it as @if(auth()->user) {{ auth()->user->name }} @else <button>login</button> @endif – Omar Abdullah Nov 16 '18 at 10:34. Laravel made it easy. One way and the most short and basic way to share data to all views is to use view:share (). Let see and example. The same above example, you have your options table in you database and you want to access its data in your master.blade.php page or any there view.

Ayou can access the auth() directly in the view.

@if(auth()->user()){{auth()->user()->name}}@endif

How to Share Data to All Views in Laravel 5.6, i read it @OmarAbdullah, Like I mentioned in the question. View share runs before the view object is created and is a key/value storage, just like Session::put(), you put your data under a specific key and you will get a same named variable across your views, a straight forward approach, nothing more needs to be said about it.

<!-- Stored in resources/views/layouts/app.blade.php -->

<html>
<head>
    <title>App Name - @yield('title')</title>
</head>
<body>
    <header>
     <div>
      @if(auth()->user()) 
          {{ auth()->user()->name }} 
       @else 
           <button>login</button> 
      @endif
      <div>
   </header>

    <div class="container">
        @yield('content')
    </div>
</body>

EDIT :

{{ auth()->user->name }} corrected -> {{ auth()->user()->name }}

How to share Variable with all Views in Laravel?, Learn to Share data to all views In Laravel. We will introduce to Laravel View Composer. The Duration: 8:26 Posted: 19 Feb 2018 View::share is typically called in a service provider, or perhaps in the constructor of a controller, so the data will be shared in views returned by that controller only. Using View::composer View composers are callbacks or class methods that are called when a view is rendered.

Sharing Data Between Views Using Laravel View Composers , How to share Variable with all Views in Laravel? We took a good look at how to pass data to views in the last section, but then what if you have a situation where​  Laravel Global variable Sharing Data with all views. Step 1 : Laravel View Files. Step 2 : Include Providers. Laravel 5.6 – Sharing variable in all views. Step 1: Create Route. Step 2 : Create Blade View. Step 3: Share Variables. Angular 6 CRUD Operations Application Tutorials. Read :

Laravel, This is all fine and dandy. Well it is until you try passing data to many views. More often than not, we need to get some data on different pages. I want to display data from the controller and throw it in the view as a dataset, but I have trouble how to write a loop on scriptjava. This is the code that I have tried, and still fails This my

Passing data to all views, View::share is typically called in a service provider, or perhaps in the constructor of a controller, so the data will be shared in views returned by that controller only. Laravel View Composer makes your views more independent since data required by the view can be available by View Composers instead of controllers. This will make your code cleaner and maintainable. 4 comments on “ Share Data in Partial Views using Laravel View Composer ”

Comments
  • just use it in your parent layout or header. laravel.com/docs/5.7/blade#template-inheritance
  • use it as @if(auth()->user) {{ auth()->user->name }} @else <button>login</button> @endif
  • i read it @OmarAbdullah, Like I mentioned in the question. but i dont know how to call it in my controller
  • First answer this. Why do you need to share it to all views ? So we get on the same page
  • because i want to show it in all view. how to use it when i share data, which is not auth
  • how to put it to AppServiceProvider from my controller
  • You don't put it from the controller. The most example of needing to do this is for the current logged in User, but it could be anything. In your AppServiceProvider, you do a query to get out any data from the DB you need, then do the View::share. So if you update the user in a controller (the name for example), your AppServiceProvider should automatically pick that up.
  • in my question, i was select data of user in controller, and i want share it to all view
  • auth() is not accessible in AppServiceProvider as appserviceprovider is loading all the service provider. At time the boot() is running there is no helper and not even AuthServiceProvider is loaded.
  • user in auth() is a method, not an attribute. you are missing the ().