Mar*_*ano 3 twitter-bootstrap laravel laravel-5 bootstrap-4 mdbootstrap
我正在做一个 Instagram 风格的小项目,但具有使用 Larvel 和 MBootstrap 的基本功能。
我正在制作表格以编辑个人信息。使用Laravel是有效的,我希望当Laravel返回表单错误时,它会在表单页面中显示错误的“Toast Notification”。
这些“toast”通知是通过单击按钮触发的,它们在 HTML 中没有正文,当您调用它们时可以使用它们。
我该怎么做才能检测到错误,无需按下按钮即可触发此“吐司”?谢谢
我知道如果 Laravel 返回错误,您需要展示祝酒词。让我提出两个解决方案。
1.使用toastr
在您的主模板文件中输入:
@if (session('error'))
<script>toastr.error('<?php echo session('error'); ?>')</script>
@endif
Run Code Online (Sandbox Code Playgroud)
并且不要忘记把它放在你的主 js 文件中:
$('.toast').toast({
delay:2000,
// Other options
});
Run Code Online (Sandbox Code Playgroud)
2.使用Bootstrap原生toast
在您的主模板文件中输入:
@if (session('error'))
<div class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img src="..." class="rounded mr-2" alt="...">
<strong class="mr-auto">Bootstrap</strong>
<small class="text-muted">11 mins ago</small>
<button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="toast-body">
Hello, world! This is a toast message.
</div>
</div>
@endif
Run Code Online (Sandbox Code Playgroud)
并且不要忘记把它放在你的主 js 文件中:
$('.toast').toast({})
Run Code Online (Sandbox Code Playgroud)