DadaArno DadaArno - 7 months ago 31
Javascript Question

SweetAlert: Alert appears twice

I have a Laravel project. I store some datas into my database, and create a notification :

public function store(CreateProjectFormRequest $request, Project $project)
$project = $request->user()->projects()->create([
'title' => $request->input('title'),
'description' => $request->input('description'),
'introduction' => $request->input('introduction'),
'city_id' => $request->input('city_id')

notify()->flash('Publié !', 'success', [
'text' => 'Votre projet a était publié avec succés !',
'timer' => 2000,

return view('')->withProject($project)->withProgress(56);

The alert appears wjen the users are redirect to the view. But, when they go to another page, the alert appears again.

This view extends my layout. This layout content this part of script:

@if (notify()->ready())
title: "{!! notify()->message() !!}",
text: "{!! notify()->option('text') !!}",
type: "{{ notify()->type() }}",
@if (notify()->option('timer'))
timer: "{{ notify()->option('timer') }}",

Please help me to fix it, I just need this alert one time. Thank you !

I got it ! I load a view instead of redirect to a route !
Juste need to edit this:

return view('')->withProject($project)->withProgress(56);


return redirect()->route('')->withProject($project)->withProgress(56);


The problem is you are creating a flash and then creating a view. So you will have the flash for this request and the following one.

When you flash a message, it will stay until the end of the next request. If you create a view (instead of redirect) you are not creating a new request, but actually "finishing" the current one. The request that will "reset" the flash will still be the next one. What you have to do then is finish this request and start another one by creating a redirect.

So you want to redirect after creating the flash, right?

return Redirect::action('YourController@yourFunction')->withProject($project)->withProgress(56);