Toastr

yoeunes/toastr image

Toastr stats

Downloads
511.2K
Stars
300
Open Issues
5
Forks
51

View on GitHub →

toastr.js flush notifications for Laravel

Toastr.js notifications for Laravel

:eyes: This package helps you to add toastr.js notifications to your Laravel projects.

Latest Stable Version Latest Unstable Version Total Downloads License

toastr

Install

You can install the package using composer

composer require yoeunes/toastr

Usage:

The usage of this package is very simple and straightforward. it only required one step to use it :

Use toastr() helper function inside your controller to set a toast notification for info, success, warning or error

// Display an error toast with no title
toastr()->error('Oops! Something went wrong!');

As an example:

<?php
 
namespace App\Http\Controllers;
 
use App\Post;
use App\Http\Requests\PostRequest;
use Illuminate\Database\Eloquent\Model;
 
class PostController extends Controller
{
public function store(PostRequest $request)
{
$post = Post::create($request->only(['title', 'body']));
 
if ($post instanceof Model) {
toastr()->success('Data has been saved successfully!');
 
return redirect()->route('posts.index');
}
 
toastr()->error('An error has occurred please try again later.');
 
return back();
}
}

Other Options

// Set a warning toast, with no title
toastr()->warning('Are you sure you want to proceed ?');
 
// Set a success toast, with a title
toastr()->success('Data has been saved successfully!', 'Congrats');
 
// Set an error toast, with a title
toastr()->error('Oops! Something went wrong!', 'Oops!');
 
// Override global config options from 'config/toastr.php'
toastr()->success('Data has been saved successfully!', 'Congrats', ['timeOut' => 5000]);

Other api methods:

You can also chain multiple messages together using method chaining

toastr()
->info('Welcome back')
->success('Data has been saved successfully!')
->warning('Are you sure you want to proceed ?');

You can use toastr('') instead of toastr()->success()

function toastr(string $message = null, string $type = 'success', string $title = '', array $options = []);

So

  • toastr($message) is equivalent to toastr()->success($message)
  • toastr($message, 'info') is equivalent to toastr()->info($message)
  • toastr($message, 'warning') is equivalent to toastr()->warning($message)
  • toastr($message, 'error') is equivalent to toastr()->error($message)

configuration:

As optional if you want to modify the default configuration, you can publish the configuration file:

php artisan vendor:publish --provider="Yoeunes\Toastr\ToastrServiceProvider"
// config/toastr.php
<?php
 
return [
 
/*
|--------------------------------------------------------------------------
| Toastr options
|--------------------------------------------------------------------------
|
| Here you can specify the options that will be passed to the toastr.js
| library. For a full list of options, visit the documentation.
|
*/
 
'options' => [
'closeButton' => true,
'closeClass' => 'toast-close-button',
'closeDuration' => 300,
'closeEasing' => 'swing',
'closeHtml' => '<button><i class="icon-off"></i></button>',
'closeMethod' => 'fadeOut',
'closeOnHover' => true,
'containerId' => 'toast-container',
'debug' => false,
'escapeHtml' => false,
'extendedTimeOut' => 10000,
'hideDuration' => 1000,
'hideEasing' => 'linear',
'hideMethod' => 'fadeOut',
'iconClass' => 'toast-info',
'iconClasses' => [
'error' => 'toast-error',
'info' => 'toast-info',
'success' => 'toast-success',
'warning' => 'toast-warning',
],
'messageClass' => 'toast-message',
'newestOnTop' => false,
'onHidden' => null,
'onShown' => null,
'positionClass' => 'toast-top-right',
'preventDuplicates' => true,
'progressBar' => true,
'progressClass' => 'toast-progress',
'rtl' => false,
'showDuration' => 300,
'showEasing' => 'swing',
'showMethod' => 'fadeIn',
'tapToDismiss' => true,
'target' => 'body',
'timeOut' => 5000,
'titleClass' => 'toast-title',
'toastClass' => 'toast',
],
];

For a list of available options, see toastr.js' documentation.

Credits

License

MIT

yoeunes photo

Symfony Certified Developer passionate about open source, Linux, and PHP.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Yoeunes Toastr Related Articles

5 Resources to Learn about the Laravel IoC Container image

5 Resources to Learn about the Laravel IoC Container

Read article
How to Create a Laravel Command image

How to Create a Laravel Command

Read article
Dependency Injection with Laravel image

Dependency Injection with Laravel

Read article
Memberships with Laravel Cashier image

Memberships with Laravel Cashier

Read article
Honeybadger logo

Honeybadger

Simple developer-focused application monitoring for Laravel. Error tracking, log management, uptime monitoring, status pages, and more!

Honeybadger
Statamic logo

Statamic

The drop-in ready Laravel CMS you’re been waiting for. Go full-stack or headless, flat file or database – it’s up to you.

Statamic
Typesense Search logo

Typesense Search

Typesense is an open source, blazing-fast search engine, optimized for helping you build delightful search experiences for your sites and apps. Natively integrated with Laravel Scout.

Typesense Search
The Certification of Competence for Laravel logo

The Certification of Competence for Laravel

A community-driven, proctored assessment across 4 levels designed to validate real-world Laravel knowledge, from Junior to mastery-level Artisan. Official Vue.js, Official Nuxt, Angular, React, JS certifications also available.

The Certification of Competence for Laravel
Celebian logo

Celebian

Celebian is a social media marketing agency specializing in helping their clients go viral on TikTok. Whether you're looking to reach a bigger audience or gain more Tiktok followers, likes, and views, they've got you covered.

Celebian
Tighten logo

Tighten

We help companies turn great ideas into amazing apps, products, and services.

Tighten