mujhtech / nav-toastr
nav-toastr flash notification with custom redirection for laravel app
Requires
- php: >=7.2.0
- illuminate/support: ~6|~7|~8
- laravel/framework: >=6.0
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
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:
-
Link to app.css
<link href="src/Toast.css" rel="stylesheet"/>
or@navtoastrCss
-
Link to app.js
<script src="src/Toast.js"></script>
or@navtoastrJs
-
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 tonavtoastr()->success($message)
navtoastr($message, 'info', true)
is equivalent tonavtoastr()->info($message, true)
navtoastr($message, 'warning', true)
is equivalent tonavtoastr()->warning($message, true)
navtoastr($message, 'error', false)
is equivalent tonavtoastr()->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