Ad

Laravel: How Do I Pass A Model To Every View If A Session Variable Is Present?

- 1 answer

In laravel, my session has a user_id in it. I'd like to fetch the row from the User's table with that id and pass it as a variable to every view.

I tried creating a service provider but the session was empty (I put it as the last service).

Any tips appreciated.

I'm not using the built in Auth module

Ad

Answer

if you want you can create a helper function that can do this for you

on your composer json file

"autoload-dev" : {
    "files" : [
         'path/to/helper.php'
     ]
}

then on your helper.php

function getActiveUser() {
   return App\User::find(session('userId'));
}

there are numerous ways you can do this. this one is one of them. . but i prefer to use the built-in auth module to handle it

Ad
source: stackoverflow.com
Ad