Showing multiple flash messages for the laravel or laravel-livewire.
Install through composer:
composer require mhmiton/laravel-flashify
Publish the package's configuration file:
php artisan vendor:publish --tag=flashify-config
Publish the package's views:
php artisan vendor:publish --tag=flashify-views
Include the package scripts in your layout file.
@flashifyScripts
or
@include('flashify::components.scripts')
or
// Laravel 7 or greater
<x-flashify::scripts />
Note: You can modify these scripts by publishing the views file.
Layout example - if Inject Plugins is enabled:
<!DOCTYPE html>
<html>
<head>
<title>Laravel Flashify</title>
</head>
<body>
<x-flashify::scripts />
</body>
</html>
Layout example - if Inject Plugins is disabled:
<!DOCTYPE html>
<html>
<head>
<title>Laravel Flashify</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/izitoast@1.4.0/dist/css/iziToast.min.css" />
</head>
<body>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script src="https://cdn.jsdelivr.net/npm/izitoast@1.4.0/dist/js/iziToast.min.js"></script>
<x-flashify::scripts />
</body>
</html>
flashify('Created', 'Data has been created successfully.');
flashify('Created', 'Data has been created successfully.', 'success', []);
or
flashify()
->plugin('swal')
->title('Created')
->text('Data has been created successfully.')
->type('success')
->fire();
or
flashify([
'plugin' => 'izi-toast',
'title' => 'Updated',
'text' => 'Data has been updated successfully.',
'type' => 'success',
]);
redirect()
->route('name')
->flashify('Created', 'Data has been created successfully.');
flashify()
->plugin('swal')
->title('Created')
->text('Data has been created successfully.')
->type('success')
->livewire($this)
->fire();
or
flashify([
'plugin' => 'izi-toast',
'title' => 'Updated',
'text' => 'Data has been updated successfully.',
'type' => 'success',
'livewire' => $this,
]);
Define preset messages in the config file "presets" key.
'presets' => [
'created' => [
'plugin' => 'swal',
'title' => 'Created',
'text' => 'Data has been created successfully.',
'type' => 'success',
'options' => [],
],
],
Show preset messages:
flashify('created');
flashify()->fire('created');
flashify([
'preset' => 'created',
]);
redirect()
->route('name')
->flashify('created');
flashify()->livewire($this)->fire('created');
flashify([
'preset' => 'created',
'livewire' => $this,
]);
LaravelFlashify.fire({
title: 'Created',
text: 'Data has been created successfully.',
type: 'success',
options: {},
});
The config file is located at config/flashify.php
after publishing the config file.
/*
|--------------------------------------------------------------------------
| Plugin Configurations
|--------------------------------------------------------------------------
|
| Sweetalert2 plugin is used by default.
|
| Supported Plugin: 'swal', 'izi-toast'
|
*/
'plugin' => 'swal',
/*
|--------------------------------------------------------------------------
| Auto-inject Plugin Assets
|--------------------------------------------------------------------------
|
| This configuration option controls whether or not to auto-inject plugin assets.
|
| By default, auto-inject is enabled.
|
| When auto-inject is enabled, the package will automatically inject the necessary
| JavaScript and CSS for plugins.
|
*/
'inject_plugins' => true,
/*
|--------------------------------------------------------------------------
| Auto Translation For The Title and Text
|--------------------------------------------------------------------------
|
| Auto Translation is enabled by default.
|
| If the trans value is true, it will be use laravel lang helper __()
| for the title and text.
|
*/
'trans' => true,
/*
|--------------------------------------------------------------------------
| Preset Messages
|--------------------------------------------------------------------------
|
| Define preset messages that will be reused.
| ---> plugin => 'plugin'
| ---> title => 'Message Title'
| ---> text => 'Message Text'
| ---> type => 'success|info|warning|error' (as per plugin)
| ---> options => {Plugin Options}
|
*/
'presets' => [
'created' => [
'plugin' => 'swal',
'title' => 'Created',
'text' => 'Data has been created successfully.',
'type' => 'success',
'options' => [],
],
.....
]
Copyright (c) 2022 Mehediul Hassan Miton mhmiton.dev@gmail.com
The MIT License (MIT). Please see License File for more information.