nikander's avatar

Redirect and flash messages

Hi!

I'm have the index pages with delete button to each element. When element deleted, I'm need to generate toast motification, but it's don't work, help me pleasem, to understand why...))

In my case, on page /data/ i show list of elements, when i'm click to delete button i'm execute destroy function, Inertia.delete(/data/${id}),

In controller i'm delete model and do redirect: return Redirect::route('data')->with('success', 'Deleted');

But my flash variable is empty and i don't understand why?

And, if I do index page on other url (for example, /list) and after redirect to data, it's worked as needed.

0 likes
7 replies
nikander's avatar
public function destroy(Data $data)
    {
        $data->delete();

        return Redirect::route('data')->with('success', 'Data deleted');
    }

For example, route /data/create work correct, for this url i'm use this function :

public function store()
    {
        Data::create([
            'name' => Request::get('name'),
            'short_desc' => Request::get('short_desc'),
            'desc' => Request::get('desc'),
        ]);

        return Redirect::route('data')->with('success', 'Data created');
    }
drehimself's avatar

Have you seen this page in the docs for flash messages? https://inertiajs.com/shared-data#flash-messages

You have to do this extra step in Inertia to show flash messages. So in this case, your session variable is named 'success'

// HandleInertiaRequests.php

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

And in your template:

<div v-if="$page.props.flash.success" class="alert">
    {{ $page.props.flash.success }}
</div>
1 like
nikander's avatar

@drehimself @lovercode

Thx, for reply, but in HandleInertiaRequest i'm already add whats needed.

It's work correctly if i redirect to other page, but if i redirect to self, it's don't work, for example, i'm on page /one, i'm submit action and in controller i'm redirect::route('two')->with('success', 'message'), it's work as needed, but when i'm do it on one page and redirect to self, it's don't work (on page /one i'm redirect::route('one')->with('success', 'message'));

1 like
drehimself's avatar

@nikander If it works on other pages but not the one you came from you probably forgot to add the template code to show it on that page.

Also, double-check Vue Devtools on that page to make sure Inertia.initialPage.props.flash.success is being set correctly.

Lara_Love's avatar

Add this you need to show alert

@if(session()->has('success'))
    <div class="alert alert-success">
        {{ session()->get('success') }}
    </div>
@endif
MacaRamos's avatar

Did you solve it, I have the same problem, and the AI ​​​​has not been able to help me

Please or to participate in this conversation.