Mohcin Bounouara
Mohcin Bounouara

Reputation: 623

Toastr not showing up in laravel 5.4

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:

code on page source

Upvotes: 1

Views: 4415

Answers (2)

Wanjila Abraham
Wanjila Abraham

Reputation: 599

I simply made sure my scripts load at the bottom of my code just before the tag, and that solved my problem. `

<!-- Scripts -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script src="{{ asset('js/app.js') }}" ></script>
<script src="{{ asset('js/toastr.min.js') }}" ></script>
<script>
    @if(Session::has('success'))

    toastr.success("{{ Session::get('success')}}")
    @endif

    @if(Session::has('info'))

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

</body>
</html>`

Initialy I had placed it inside the tag

Upvotes: 0

itsmysterybox
itsmysterybox

Reputation: 2838

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

Upvotes: 4

Related Questions