Home > Software engineering >  Want to show Sweet Alert for " New Data Added " and then return home page
Want to show Sweet Alert for " New Data Added " and then return home page

Time:02-27

I need help to show the sweet alert when new data added and return back to home page.

Controller - Data save and redirect to page

     public function addbuffaloData(Request $req) 
       {
        ......
        $data->save ();
        $inspectiondata-> save ();
        $vaccinationdata->save();

        return redirect('/buffalo-details'); 
        }

CodePudding user response:

Flash it to session using _flash, there are couple of ways, use this if you wish:

// ......
$data->save();
$inspectiondata->save();
$vaccinationdata->save();

return redirect()
            ->route('buffalo.details')
            ->with('message', [
                'type' => 'Success',
                'text' => 'Added successfully',
            ]);

Remember to name your route /buffalo-details as:

// For your route
Route::get('/buffalo-details', /*SomeController/*)
// Add this line:
->name('buffalo.details');

And in your master view blade file:

<script type="text/javascript">
    // SweetAlert Toast example
    function showToast(type, message) {
        window.toast.fire({
            icon: type,
            type: type,
            title: message,
        });
    }

    // SweetAlert Alert Example
    function showAlert(title, message, type) {
        window.swal.fire({
            title: "Response Message",
            text: title,
            type: type,
            confirmButtonText: "OK",
            html: message,
            // cancelButtonText: "Cancel",
            showCancelButton: false,
        }).then(console.log)
            .catch(console.error);
    }

    @if(session()->has('message'))
        showToast({{ strtolower(session('message')['type']) }}, {{ session('message')['text'] }});
    @endif

</script>

  • Related