首页 > 解决方案 > 我想在 laravel 8 中更新记录后显示警报消息

问题描述

在Controller文件中:return redirect()->route('show_record')->with('update', '内容更新成功!');

在查看文件中:

@if(session('update'))
<div class="alert alert-success alert-dismissable custom-success-box" style="margin: 15px;">
    <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
    <strong> {{ session('update') }} </strong>
</div>
@endif

标签: alertlaravel-8

解决方案


我已经在我建立的许多网站上实现了这种东西,并且倾向于使用 Noty 来进行通知,因为它实现起来相对简单。将它加载到每个页面上,并在您的控制器中,如果您希望返回消息,请执行此操作。

在您的控制器中:

    $notification = array(
        'message' => 'Message wording goes here',
        'alert-type' => 'success / danger / warning / info etc.'
    );
    return redirect()->route('your.route.here')->with($notification);

然后在网站前面的脚本部分,确保加载 Noty.js 后:

@if(Session::has('message'))
<script>
$(document).ready(function(){
    var type = "{{ Session::get('alert-type', 'info') }}";
    var message = "{{ Session::get('message') }}";
    new Noty({
        text: message,
        type: type
    }).show();
});
</script>
@endif

推荐阅读