3

Need help for Showing Flash Message after adding / Deleting Data using Ajax in Laravel 8 i included the file "flash-message.blade.php"

<div class="content-wrapper">
  @include('flash-message')

My Controller File

 public function deleteData(Request $req) 
    {
        $deletedata = buffalodata::destroy($req->id); 
         return response()->json( $deletedata );
    }

Ajax Call for Delete Data

    $(document).on('click', '#footer_delete_button', function() { 

            $.ajax({

                type:'post',
                url: '/deleteData',
                data:{
                    '_token': $('input[name=_token]').val(),
                    'id'    : $('.did').text()
                    },

                        success: function(data) {
                            console.log("Success");
                            $('.item' + $('.did').text()).remove();
                                
                                location.reload()
                        }
                });


            });

Thanks in Advance....

1 Answer 1

1

You can use https://github.com/stanlemon/jGrowl after deleting successfully

I have used this as shown below:

Add this script in your layout.blade.php file

function popUpMessage(colorTheme, message) {
        $.jGrowl(message,
            {
                theme: colorTheme
            }
        );
    }

For success call this:

popUpMessage('bg-success', "Deleted successfully");

For error call this:

popUpMessage('bg-danger', "Some error occured");

'bg-success' and 'bg-danger' are bootstrap classes, you can use your own

If you want to flash session messages from controller then you can use:

Add this to your flash-message.blade.php

@if (session()->has('success'))
    <script>
        popUpMessage('bg-success', "{{ session()->get('success') }}" );
    </script>
@elseif (session()->has('error'))
    <script>
        popUpMessage('bg-danger', "{{ session()->get('error') }}");
    </script>
@endif

Then after successfully deleting you can call this form controller:

    session()->flash('success', 'Deleted Successfully');

For error

    session()->flash('error', 'Some error occurred');

As you are using ajax then don't use location.reload()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.