Session Create in laravel

Session Create in laravel

Friends, before telling you what is a session in Laravel, I have to tell you that what actually is a session is that we store some data temporarily inside our cookies, that is, we store it inside our browser, this is called session data.

We do this so that we can get the details of the user based on the session data and we can set the data of what the user has done till now on our website or web application.

There is a very easy way to use a session in Laravel and the way to get its data is also very easy and this is how to create a session in Laravel:

 

Controller:

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

if ($request->session()->has('key')) {
    $langs = session('langs'); echo $langs;
}
View:

@if(!empty(Session::get('userid')))

<form method="POST" action="{{ route('logout') }}">
@csrf
<button type="submit" class="btn btn-danger">
Logout
</button>
</form>

@else <a href="/login"> Login </a> @endif

Leave a Reply