Search code examples
phplaravelsweetalert

Confirm and Cancel Button Laravel Sweet Alert


Good Morning:

I am trying to use plugin Sweet Alert in Laravel. My problem is, I don´t know the how to put a Alert with Cancel Button and Confirm Button in Controller. Reading the documentation, I only found this sentence:

Alert::warning('Are you sure?', 'message')->persistent('Close');

Also, I trying with:

echo '<script>swal({ title: "Are you sure?", text: "name is available, continue save?", type: "warning", showCancelButton: true, confirmButtonColor:"#DD6B55",  confirmButtonText: "Yes, delete it!",   closeOnConfirm: false }, function(){});</script>';

But it not working.

How can I use Sweet Alert in my Controller showing a Cancel Button and Confirm Button?

Thanks


Solution

  • If you are using this repo then I insist you to read the docs thoroughly as it is given how generate alerts with proper snippets You are basically looking for this

    Snippet from the repo itself

    Controller

    Your controller should look like this

    public function yourfunction()
    {
        Alert::warning('Are you sure?', 'message')->persistent('Close');
    
        return to whatever view    
       //return redirect::home();
    }
    

    View

    Inside the view where you are redirecting from the store method you should do something like as given in repo

    @if (Session::has('sweet_alert.alert'))
        <script>
            swal({
                text: "{!! Session::get('sweet_alert.text') !!}",
                title: "{!! Session::get('sweet_alert.title') !!}",
                timer: {!! Session::get('sweet_alert.timer') !!},
                type: "{!! Session::get('sweet_alert.type') !!}",
                showConfirmButton: "{!! Session::get('sweet_alert.showConfirmButton') !!}",
                confirmButtonText: "{!! Session::get('sweet_alert.confirmButtonText') !!}",
                confirmButtonColor: "#AEDEF4"
    
                // more options
            });
        </script>
    @endif
    

    PS:: I haven't used sweet alerts yet but this repo is cool! it has all you want to know about sweet alerts!