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

mstdmstd's avatar

How to set flash_type when setting flash message in control?

In laravel 9 with Inertiajs 3 I try to set flash_type when setting flash message in control :

return redirect(route('admin.dashboard.index'))
    ->with( 'flash', 'You have no access to currency listing')
    ->with('flash_type', 'error12');

In app/Http/Middleware/HandleInertiaRequests.php I added lines :

public function share(Request $request): array
{
    \Log::info(  varDump($request->session()->get('flash_type'), ' -1 HandleInertiaRequests $request->session()->get(\'flash_type\')::') ); // I SEE VALID 'error12' value
    return array_merge(parent::share($request), [

        'flash' => [
            'message' => fn () => $request->session()->get('message')
        ],
        'flash_type' => [
            'message' => fn () => $request->session()->get('flash_type')
        ],
        'auth' => function() {
            $user = auth()->user();

But checking on client side I see that flash_type value not in this.$page.props.jetstream : https://prnt.sc/3PdFiF6Dzm7a

How to set flash_type corectly ?

Thanks !

0 likes
9 replies
Nakov's avatar

I am just curious on which code you are looking at, and does that parent::share has to do something with what you see on the front-end.. Because in your code above for example, you have this:

$request->session()->get('message')

for the flash message, while you are passing it from the redirect in a flash key.. So something is going on there. And also I don't see flash.message or whatever else on the front-end.

mstdmstd's avatar

@Nakov , I am not sure about your message. Originally app/Http/Middleware/HandleInertiaRequests.php has defined :

    /**
     * Defines the props that are shared by default.
     *
     * @see https://inertiajs.com/shared-data
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function share(Request $request): array
    {
        return array_merge(parent::share($request), [
            //
        ]);
    }

I suppose there are some default data which with parent::share is filled into JS side.

I managed to pass 'flash=>message' and 'auth' blocks from laravel into JS side successfully. In my printscreen I see flash.message data. Maybe it has invalid format ?

I can not define in HandleInertiaRequests 2 keys flash and flash_type ?

Nakov's avatar

@mstdmstd My message is regarding this:

'flash' => [
    'message' => fn () => $request->session()->get('message')
],

this part in particular: $request->session()->get('message') you are getting something from the session under the key message while from your controller you use this: ->with( 'flash', 'You have no access to currency listing') so in the session it should be under the flash key not message.

So that's why I am saying are you using/looking at the correct place from where you are passing the data.

In your inertial middleware add this:

'flash_type' => [
    'message' => 'Testing if it will show'
],

just to make sure that you'll get the message in the console. If you do, then it means that from your controller this is not passed probably because you are not hitting that endpoint, OR something else happens in between the requests, and since it is a flash session (lasting for only one request) the message is gone.

Hope this helps.

1 like
mstdmstd's avatar

@Nakov Modifying lines in app/Http/Middleware/HandleInertiaRequests.php :

        return array_merge(parent::share($request), [
            'flash' => [
                'message' => fn () => $request->session()->get('message')
            ],
            'flash_type' => [
                'message' => 'Testing if it will show'
            ],

I do not see flash_type message oin browser's console output of this.$page.props.jetstream , but I see valid message in 'flash' Just as in my printscreen above.

Nakov's avatar

@mstdmstd Can you try this:

return array_merge(parent::share($request), [
            'flash' => [
                'message' => fn () => 'Some testing message here'
            ],
            'flash_type' => [
                'message' => fn () => 'Testing if it will show'
            ],

And do you see the message from above as the flash message? Send a screenshot from the console again..

mstdmstd's avatar

@Nakov Full code of my method : public function share(Request $request): array { return array_merge(parent::share($request), [

        'flash' => [
            'message' => fn () => 'Some testing message here 123'
        ],
        'flash_type' => [
            'message' => fn () => 'Testing if it will show'
        ],
        
        'auth' => function() {
            $user = auth()->user();
            $site_name = Settings::getValue('site_name', CheckValueType::cvtString, 'Admin demo');

            return $user ? [
                'profile' => $user,
                'site_name' => $site_name,
                'notifications' => [],// $user->notifications,
                'readNotifications' => [],// $user->readNotifications,
                'unreadNotifications'=> [],// $user->unreadNotifications,
            ] : null;
        }
    ]);
}

but in console these text is not applied : https://prnt.sc/1oPDy0M-_Ytm

Nakov's avatar

@mstdmstd I proved my point that you are trying to add the message in a wrong place, search your project to where else you have 'flash' => and you’ll see where does that message comes from.

1 like
mstdmstd's avatar

@Nakov I show it in my first post. Which is valid syntax if I see 'flash' message, but DO NOT see 'flash_type' ?

Nakov's avatar
Nakov
Best Answer
Level 73

@mstdmstd You changed your flash to say this:

'flash' => [
            'message' => fn () => 'Some testing message here 123'
        ],

but look at your screenshot, the flash says You have no access to this listing, right? Which means the code above has nothing to do with what is shown on the front-end. You need to find from where does that message comes from, and you can add the flash_type there.

1 like

Please or to participate in this conversation.