tjgazel/laravel-toastr

Toastr notifications for laravel 5.5+, 6 and 7

v1.1.0 2020-09-04 00:22 UTC

This package is auto-updated.

Last update: 2024-11-04 09:32:05 UTC


README

Latest Stable Version License Total Downloads


Other packages:


Installation

1) Install the Toastr library (front end dependency)

1.1) Install Toastr via npm . Github | Demo

npm install toastr --save-dev

1.2) Require the js in resources/assets/js/bootstrap.js as window.toastr = require('toastr');

try {
  window.$ = window.jQuery = require('jquery');

  require('bootstrap');

  window.toastr = require('toastr');

} catch (e) { }

1.3) Import the sass in resources/assets/sass/app.scss as @import "~toastr/toastr"; then build via npm npm run prod.

// Fonts
@import url("https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700");

// Variables
@import "variables";

// Bootstrap
@import "~bootstrap/scss/bootstrap";

// Toastr
@import "~toastr/toastr";



2) Install tjgazel/laravel-toastr

2.1) Run

composer require tjgazel/laravel-toastr

2.2) Optional: Laravel 5.4 and below
Add TJGazel\Toastr\ToastrServiceProvider::class to providers in config/app.php.
Add 'Toastr' => TJGazel\Toastr\Facades\Toastr::class to aliases in config/app.php.

// config/app.php

'providers' => [
  // ...
  TJGazel\Toastr\ToastrServiceProvider::class,
],

'aliases' => [
  // ...
  'Toastr' => TJGazel\Toastr\Facades\Toastr::class,
],

2.3) Run php artisan vendor:publish --provider="TJGazel\Toastr\ToastrServiceProvider" to publish the config file in config/toastr.php


2.4) Add {!! Toastr::render() !!} Facade or {!! toastr()->render() !!} helper in your main view.

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="/css/app.css">
</head>
<body>
    <div id="app"></div>
    <script type="text/javascript" src="js/app.js"></script>
    {!! toastr()->render() !!}
</body>
</html>

2.5) Optional: Modify the publish configuration file locate at config/toastr.php to your liking.

<?php

return [

    'session_name' => 'toastr',

    'options' => [
        "closeButton" => true,
        "debug" => false,
        "newestOnTop" => false,
        "progressBar" => true,
        "positionClass" => "toast-bottom-right",
        "preventDuplicates" => false,
        "onclick" => null,
        "showDuration" => "300",
        "hideDuration" => "1000",
        "timeOut" => "10000",
        "extendedTimeOut" => "1000",
        "showEasing" => "swing",
        "hideEasing" => "linear",
        "showMethod" => "fadeIn",
        "hideMethod" => "fadeOut"
    ]

];



Usage

Use the Toastr facade Toastr:: or the helper function toast()-> to access the methods in this package.

Toastr::error(string $message, string $title = null, array $options = []);

toastr()->error(string $message, string $title = null, array $options = []);

Examples:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use TJGazel\Toastr\Facades\Toastr;

class DashboardController extends Controller
{

    public function index()
    {
        Toastr::info('welcome admin!');

        return view('dashoard.index');
    }
}

You can also chain multiple messages together using:

toastr()
    ->error('Unauthorized!', 'Error 401')
    ->info('Authentication required to access dashboard page', null, ['timeOut' => 15000]);

Note that in the 3rd parameter you can customize toastr options. See the config/toastr.php file for all possibilities.


All methods

toastr()->info();
toastr()->warning();
toastr()->success();
toastr()->error();

Tanks for:

Toastr
Laravel

Author: Talles Gazel
Home page
Facebook

Tutorial video