Toastr not showing up in laravel 5.4

人走茶凉 提交于 2019-12-24 07:59:44

问题


I tried to use Toast notification, I think I have included everything alright. It can be seen on page source that generated after the action, but it's not showing the notification on the page.

The includes:

<link href="{{ asset('css/toastr.min.css') }}" rel="stylesheet">
<script src="{{ asset('js/toastr.min.js') }}"></script>

The call:

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

The controller:

$this->validate($request, [
  'name' => 'required'
]);

$category = new Category; // create instance

$category->name = $request->name; //  take name from form request
$category->save(); // save it to database

Session::flash('success', 'Category Has Been Created :D');

return redirect()->route('categories');

and the following image shows that code has passed with success:


回答1:


I had this problem recently. To solve that, I made sure following:

(It's possibly point 4 in your case)

1) Link toastr.css and toastr.js in your layout file

<link href="{{ asset('css/toastr.min.css') }}" rel="stylesheet">
<script src="{{ asset('js/toastr.min.js') }}"></script>

2) Do not use defer attribute in markup

<script src="{{ asset('js/toastr.min.js') }}" defer></script>

It should be:

<script src="{{ asset('js/toastr.min.js') }}"></script>

3) Import jQuery

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>

4) Load jQuery before toastr

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script src="{{ asset('js/toastr.min.js') }}"></script>

5) Make sure the spellings, functions, etc. are correct



来源:https://stackoverflow.com/questions/51809850/toastr-not-showing-up-in-laravel-5-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!