【问题标题】:toastr notification not work using laravel8使用 laravel 8 的 toastr 通知不起作用
【发布时间】:2020-10-14 20:24:02
【问题描述】:

我在 laravel 8 中使用 toastr 我在控制器中的代码 我所有的代码都在这里,希望大家有所帮助

$flight = new contact;
        
$flight->name = $request->name;
$flight->email = $request->email;
$flight->message = $request->message;
$flight->save();
        
$notification = array(
    'message' => 'Message send Successfully', 
    'alert-type' => 'success'
);

return redirect('/')->with($notification);

和javascript文件

<script>
    @if (Session::has('message'))
        var type = "{{ Session::get('alert-type', 'info') }}";
        switch(type)
        {
            case 'info':
                toastr.info("{{ Session::get('message') }}");
                break;
                                
            case 'warning':
                toastr.warning("{{ Session::get('message') }}");
                break;
                        
            case 'success':
                toastr.success("{{ Session::get('message') }}");
                break;
                        
            case 'error':
                toastr.error("{{ Session::get('message') }}");
                break;
        }
    @endif    
</script>

保存数据后不显示警报但数据存在于数据库中

【问题讨论】:

    标签: php laravel laravel-8


    【解决方案1】:

    您不是在向会话闪烁,而是将数据传递给视图。试试这个吧。

    $notification = [
        'message' => 'Message send Successfully', 
        'type' => 'success'
    ];
    
    session()->flash('notification', $notification);
    
    return redirect('/');
    

    和javascript文件

    <script>
        @if (Session::has('notification'))
            switch('{{ Session::get('notification')['type'] }}')
            {
                case 'info':
                    toastr.info('{{ Session::get('notification')['message'] }}');
                    break;
                                    
                case 'warning':
                    toastr.warning('{{ Session::get('notification')['message'] }}');
                    break;
                            
                case 'success':
                    toastr.success('{{ Session::get('notification')['message'] }}');
                    break;
                            
                case 'error':
                    toastr.error('{{ Session::get('notification')['message'] }}');
                    break;
            }
        @endif    
    </script>
    

    【讨论】:

    • 我有这个错误 htmlspecialchars() 期望参数 1 是字符串,给定数组
    • 如果有办法不使用 JSON
    猜你喜欢
    • 2021-02-08
    • 1970-01-01
    • 2019-07-25
    • 2017-06-10
    • 1970-01-01
    • 2023-01-23
    • 2013-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多