Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use session in custom class laravel 5?

here is my code:

<?php 
use Illuminate\Support\Facades\Session;
namespace App\CustomLibrary

{
    class myFunctions {
        public function is_login() {
            if(Session::get('id') != null){

                return TRUE;
            }
        }
    }
}
?>

I'm new in laravel 5, i just added a new custom function. And inside that function i wanna check a session ('id)? But i've got an error like this

FatalErrorException in myFunctions.php line 8: Class 'App\CustomLibrary\Session' not found

I need to know how to use session properly.

like image 729
Reynald Henryleo Avatar asked Feb 05 '23 19:02

Reynald Henryleo


2 Answers

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)
like image 153
Alexey Mezenin Avatar answered Feb 08 '23 04:02

Alexey Mezenin


Your use needs to be after the namespace declaration:

//use Illuminate\Support\Facades\Session; //Not here
namespace App\CustomLibrary

use 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.

like image 34
apokryfos Avatar answered Feb 08 '23 04:02

apokryfos