0

When i wrote this in command line composer require brian2694/laravel-toastr. I've got message like [InvalidArgumentException]

Could not find package brian2694/laravel-toastr. It was however found via repository search, which indicates a consistency issue with the repository.

Any tips to solve this problem ?

2
  • which version you are using of laravel ? Commented Dec 6, 2021 at 4:27
  • im using laravel 8
    – Nico Aramy
    Commented Dec 7, 2021 at 9:25

1 Answer 1

1

First thing, you need to include the Toastr library to Laravel blade views. You can achieve this by below methods.

1.Include css and js file from a CDN

2.Install the library using NPM

3.Download the css and js file from github Toastr Repository

However you choose the grab the library, you have to add the location to the 2 files in the head tag of your master layout file like so:

<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.1.4/toastr.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.1.4/toastr.min.css">

After including the library, whenever you need to redirect to a page most probably from a controller, you can pass the notification information like so:

$notification = array(
    'message' => 'Post created successfully!',
    'alert-type' => 'success'
);

return Redirect::to('/')->with($notification);

After adding redirect notification to your controller, add following javascript at the bottom of your layout file (app.blade.php or master.blade.php – depends on how you name it).

 @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

Note* Please open and close script tag before and after the above code snippet respectively.

That’s it. Now if you visit or perform the action you have targeted, you will be shown a nice notification using toastr. You can set the alert type from info, warning, success or error and correct colored notification will be shown.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.