mujhtech/nav-toastr

nav-toastr flash notification with custom redirection for laravel app

v1.10 2021-06-25 09:27 UTC

This package is auto-updated.

Last update: 2024-11-25 17:22:42 UTC


README

👀 This package helps you to add Toast.js notifications to your Laravel app

Latest Stable Version Latest Unstable Version Build Status Scrutinizer Code Quality Code Coverage Total Downloads License

Install

You can install the package using composer

$ composer require mujhtech/nav-toastr

Then add the service provider to config/app.php. In Laravel versions 5.5 and beyond, this step can be skipped if package auto-discovery is enabled.

'providers' => [
    ...
    Mujhtech\NavToastr\NavToastrServiceProvider::class
    ...
];

To install the configuration and assets file run:

$ php artisan navtoastr:install"

Usage:

Include [app.css] and app.js in your view template:

  1. Link to app.css <link href="src/Toast.css" rel="stylesheet"/> or @navtoastrCss

  2. Link to app.js <script src="src/Toast.js"></script> or @navtoastrJs

  3. use navtoastr() helper function inside your controller to set a toast notification for info, success, warning or error

// Display an info toast with no title
navtoastr()->info('Are you the 6 fingered man?')

as an example:

<?php

namespace App\Http\Controllers;

use App\User;
use App\Http\Requests\PostRequest;
use Illuminate\Database\Eloquent\Model;

class UserController extends Controller
{
    public function store(UserRequest $request)
    {
        $post = User::create($request->only(['username', 'password']));

        if ($post instanceof Model) {

            navtoastr()->success('Data has been saved successfully!');

            return navtoastr()->named('posts.index');
        }

        navtoastr()->error('An error has occurred please try again later.');

        return navtoastr()->back();
    }
}

After that add the @navtoastrRender at the bottom of your view to actualy render the nav-toastr notifications.

<!doctype html>
<html>
    <head>
        <title>Nav Toastr.js</title>
        @navtoastrCss
    </head>
    <body>
        
    </body>
    @navtoastrJs
    @navtoastrRender
</html>

Other Options

// Set a info toast
navtoastr()->info('My name is Muhideen Mujeeb')

// Set a success toast
navtoastr()->success('Have fun storming the castle!')

// Set an error toast
navtoastr()->error('I do not think that word means what you think it means.')

// Set an warning toast

navtoastr()->warning('We do have the Kapua suite available.')

Other api methods:

// You can also chain multiple messages together using method chaining

navtoastr()->info('Are you the 6 fingered man?')->success('Have fun storming the castle!')->warning('doritos');

// you could replace @navtoastrRender by :

navtoastr()->render() or app('nav-toastr')->render()

// you can use navtoastr('') instead of navtoastr()->success()

function toastr(string $message = null, string $type = 'success', string $title = '', bool $enableCustomButton = false);

so

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

configuration:

// config/nav-toastr.php
<?php

return [
    
    'custombuttons' => [
        [
            'text'      => 'Refresh the page',
            'reload'    => true
        ],
        [
            'text'      => 'My Website',
            'url'       => 'https://mujh.tech'
        ],
        [
            'text'      => 'Twitter',
            'url'       => 'https://twitter.com/mujhtech'
        ]
    ],
];

Credits

License

MIT