How to use session in custom class laravel 5? How to use session in custom class laravel 5? laravel laravel

How to use session in custom class laravel 5?


Add this clause to the top of the class, right after namespace part:

use Session;

Or use full namespace:

if (\Session::get('id') != null)

Or use the session() helper:

if (session('id') != null)


Your use needs to be after the namespace declaration:

//use Illuminate\Support\Facades\Session; //Not herenamespace App\CustomLibraryuse Illuminate\Support\Facades\Session; //Here

Anything that is used before the namespace is used within the global namespace which changes once a namespace is declared.


use Session; in Model and Controller

// Via a request instance...

$request->session()->put('key', 'value');

// Via the global helper...

session(['key' => 'value']);

for more details https://laravel.com/docs/5.1/session