2017-10-21 95 views
1

我试图用toastr显示多个通知,但只显示第一条消息。如何在laravel中显示多个Toast通知

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

输出:

{状态: “成功”,邮件: “订舱修改成功!” }

消息:“Booking successfully modified!”

状态:“成功”

enter image description here

我将不胜感激,如果你能告诉我如何展示多个消息如在上面的截图。有些通知可能会有警告状态,有些可能会有成功状态。

回答

0

你这样做的方式覆盖了数组的statusmessage属性。

考虑创建数组的数组这样的事情:

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

然后就可以遍历每个元素。

$.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); 
    } 
}); 
+0

感谢您的回复。不幸的是,我只收到一个空的错误通知。我认为括号导致了一个问题,但如果我删除括号我得到HTTP状态代码“1”不valid.error。有没有另一种方法可以尝试? – Johnny

+0

我将更新旧版本的PHP的答案 – Lloople

+0

对不起,它也没有工作。我更新了我的问题。增加了一个输出。您可能会更好地了解当前数组的外观。 – Johnny