【问题标题】:Toastr message is not working in laravel-8Toastr 消息在 laravel-8 中不起作用
【发布时间】:2021-02-08 11:54:23
【问题描述】:

我刚刚在表单提交时添加了一个 toast,但它不起作用,我不明白这个问题。

public function create(Request $data)
{

    $data->validate([
        'name'          =>  'required|string|max:255',
        'phone'         =>  'required|unique:users|numeric|digits:11',
        'cnic'          =>  'nullable|unique:users|numeric|digits:13',
        'email'         =>  'required|string|email|max:255|unique:users',
        'designation'   =>  'nullable|string',
        'department'    =>  'nullable|string',
        'password'      =>  'required|string|min:4',
    ]);

    User::create([
        'name'          => $data['name'],
        'phone'         => $data['phone'],
        'cnic'          => $data['cnic'],
        'email'         => $data['email'],
        'role'          => $data['role'],
        'designation'   => $data['designation'],
        'department'    => $data['department'],
        'password'      => Hash::make($data['password']),
    ]);

    return redirect()->route('manage-users')->with('message', 'User created successfully!');
}

这是我的控制器,我在刀片中使用这种方式

@if(Session::has('message'))
    toastr.info("{{ Session::get('message') }}");
@endif

这是css:

@section('vendor-style')
{{-- vendor css files --}}
<link rel="stylesheet" href="{{ asset('vendors/css/extensions/toastr.css') }}">
@endsection

@section('page-style')
{{-- Page Css files --}}
<link rel="stylesheet" href="{{ asset('css/plugins/extensions/toastr.css') }}">
@endsection

和js文件:

@section('vendor-script')
{{-- vendor files --}}
<script src="{{ asset('vendors/js/extensions/toastr.min.js') }}"></script>
@endsection

@section('page-script')
{{-- Page js files --}}
<script src="{{ asset('js/scripts/extensions/toastr.js') }}"></script>
@endsection

这些文件工作正常。感谢您的帮助!

【问题讨论】:

  • 还是不行!
  • 你的head标签下是否加载了tosstr cdn?
  • 是的,所有文件都正确连接
  • 会话有消息?用@if(Session::has('message')) alert("hello"); @endif检查它
  • 没有警报没有显示

标签: php laravel laravel-blade laravel-8


【解决方案1】:

因为它是一个 javascript 库,所以您需要将此代码放入 &lt;script&gt; 标记中

@if(Session::has('message'))
    toastr.info("{{ Session::get('message') }}");
@endif

@if(Session::has('message'))
    <script>
        toastr.info("{{ Session::get('message') }}");
    </script>
@endif

如果仍然不工作意味着它没有触发你的代码,所以你可以这样做

@if(Session::has('message'))
<script>
    $(function(){
            toastr.info("{{ Session::get('message') }}");
        })
</script>
@endif

页面加载后,此代码将触发此代码

【讨论】:

  • 非常感谢您的帮助,但问题仍然存在。
  • 是的,当我在控制台中编写此代码时,它会显示 toastr
  • 我试试这个警报显示但不烤面包!&lt;script&gt; var msg = '{{Session::get('message')}}'; var exist = '{{Session::has('message')}}'; if(exist){ toastr.info(msg); // alert(msg); } &lt;/script&gt;
  • 你能把刀片文件代码放在你的问题中吗?
【解决方案2】:

我为高栈创建了一个包(Laravel 8 并将保持更新)。请随意查看:

https://github.com/usernotnull/tall-toasts

【讨论】:

    猜你喜欢
    • 2017-06-10
    • 1970-01-01
    • 1970-01-01
    • 2016-08-17
    • 2015-07-05
    • 2021-03-20
    • 1970-01-01
    • 1970-01-01
    • 2021-03-13
    相关资源
    最近更新 更多