Johnny
Johnny

Reputation: 416

how to show multiple toast notifications in laravel

I am trying to show multiple notifications with toastr but only first message is being shown.

return response()->json(array(
                             'status'=>'warning',
                             'message'=> 'Invoiced could not sent!',
                             'status'=>'success',
                             'message'=> 'item successfully modified!'
));

Output:

{ status: "success", message: "Booking successfully modified!" }

message: "Booking successfully modified!"

status: "success"

enter image description here

I would appreciate if you can tell me how to show multiple messages as in the above screenshot. some of the notifications might have a warning status and some might have a success status.

Upvotes: 2

Views: 2720

Answers (1)

Lloople
Lloople

Reputation: 1844

The way you're doing it is overriding the status and message properties of the array.

Consider creating an array of arrays for such a thing:

return response()->json(array(
    'messages' => array(
        array(
            'status'=>'warning',
            'message'=> 'Invoiced could not sent!'
        ),
        array(
            'status'=>'success',
            'message'=> 'item successfully modified!'
        )
    )
));

Then you can iterate over each element.

$.ajax({
    // Your current configuration
}).done(function (response) {
    for(var msg in response.messages) {
        var status = response.messages[msg]['status'];
        var message = response.messages[msg]['message'];
        toastr[status](message);
    } 
});

Upvotes: 2

Related Questions