Search code examples
phplaravellaravel-blade

The redirect with->('message') in laravel show the toast before the redirect


I'm trying to show a message after I redirect to a page in laravel but the message show just before the reload of the page. I want the message to show on the page after the redirect. Here is my Controller code :

 public function update(UpdateItem $request, $id)
    {
        $vd = $request->validated();
        $item = $this->itemService->updateItem(json_decode($vd['data'], true), $id, $vd['modele_id']);

        if ($request->wantsJson()) {
            return ['item' => $item];
        } else {
            return redirect()->route('item-show', ['id' => $id])->with('message', "Objet modifié");
        }
    }

Here is my blade code:

@if($messages = session('messages') )
    <script>
        @if( is_array($messages) )
        @foreach ( $messages as $message )
        window.showSuccess('{{ $message }}')
        @endforeach
        @else
        window.showSuccess('{{ $messages }}')
        @endif
    </script>
@endif

When I update my item, it shows the toast with the text "Objet modifié" for half a second then it redirects to the page with the route "item-show".

How can I have the toast to show after the redirect ?

Thank you


Solution

  • Well I just had to rename "message" to "messages"