'Laravel realrashid/sweetalert not working

I am trying to install SweetAlert in my Laravel project so I ran this commands

composer require realrashid/sweet-alert

php artisan sweetalert:publish

And inside my controller I made this

public function handleIcityNewCairoInterest(Request $request)
{
    $first_name = $request->first_name;
    $last_name = $request->last_name;
    $email = $request->email;
    $phone = $request->phone;
    $project = $request->project;
    $unit = $request->unit;
    $budget = $request->budget;
    $installment = $request->installment;

    IcityNewcairoInterest::create([
        'first_name' => $first_name,
        'last_name' => $last_name,
        'email' => $email,
        'phone' => $phone,
        'project' => $project,
        'unit' => $unit,
        'budget' => $budget,
        'installment' => $installment
    ]);
    Alert::success('Success', 'Done!');
    return redirect(route('home'));
}

But when I submit the form it navigates me to the home page without pop up any alerts

Can anyone help me please?



Solution 1:[1]

Alert won't work for you because of redirection. You can send data to your redirect route. So, on your return redirect line add with() function and send some data like:

return redirect(route('home'))->with('success', 'Done!');

and after that in your view file of homepage, because you are redirecting to your home. add this:

@if (\Session::has('success'))
   @php alert()->success('Success', \Session::get('success')); @endphp
@endif

Also, you may call alert function in your controller, on your homepage's function, (f.e pretending that your homepage's controller named getHomepage):

use RealRashid\SweetAlert\Facades\Alert;

public function getHomepage()
{
... //The rest of your functions code

   if (\Session::has('success')){
      alert()->success('Success', \Session::get('success'));
   }

... //The rest of your functions code
}

Hope it will help, i didn't test it out

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1