Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Alphal1234's avatar

Set a session in laravel blade

Guys I have a question, How can I set a session in laravel blade?

0 likes
13 replies
tomopongrac's avatar

You can like this

<?php
session(['key' => 'value']);
?>

but It is not recommended to do that inside blade templates, blade’s purpose is to render the view.

munazzil's avatar

You can just add this code in app.blade.php or your main page

   @if ($message = Session::get('error'))
    <div class="alert alert-success">
        <p>{{ $message }}</p>
    </div>
     @endif
1 like
tomopongrac's avatar

In controller which render your blade file put this code ... for example

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    /**
     * Show the profile for the given user.
     *
     * @param  Request  $request
     * @param  int  $id
     * @return Response
     */
    public function show(Request $request, $id)
    {
        $value = $request->session()->put('key', 'value');

        //
    }
}

For more explanation check the documentation https://laravel.com/docs/5.7/session#storing-data

Alphal1234's avatar

@TOMOPONGRAC - no i meant, the session that I created in blade, how can I use it in controller? Should I pass in route ? That it the thing I don't get.

I have this situation. User has to make a booking. If the user is not logged in I have to set a session with the $event->id. Then after he logs in should be redirected to booking form blade.

But I have no idea how to send this session in LoginController... :/

Nee's avatar

Just call it the same way you would.

You don't have to send it through your route.

It would already be available in the controller once you set it in the blade file.

In your controller, call this with the key you gave it in the blade file.


Session::get($key);

tomopongrac's avatar
Level 51

In your LoginController try to add this method

    protected function authenticated(Request $request, $user)
    {
        if ($request->session()->has('key')) {
        $sessionData = $request->session()->get('key');
            return redirect()->route('your route');
        }

        return redirect($this->redirectTo);
    }
1 like
Alphal1234's avatar

@TOMOPONGRAC - I need your help again! I am doing the same thing in RegisterController but it doesn't redirect? What can i do ?

tomopongrac's avatar

In RegisterController create registered method

    /**
     * The user has been registered.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  mixed  $user
     * @return mixed
     */
    protected function registered(Request $request, $user)
    {
        if ($request->session()->has('key')) {
        $sessionData = $request->session()->get('key');
            return redirect()->route('your route');
        }

        return redirect($this->redirectTo);
    }
1 like
LaravelNovice:)'s avatar

Hi. I have something different situation related to sessions.

I need to store some value in the blade and then router to another blade file and use that session value. How can I do it ?

Please or to participate in this conversation.