Flash
Toast notifications use the session mechanism (session()->flash()
) and can be easily triggered directly from controllers.
use MoonShine\Support\Enums\ToastType;MoonShineUI::toast(message: 'Hello');
use MoonShine\Support\Enums\ToastType;MoonShineUI::toast(message: 'Hello');
You can also specify the notification type and duration:
MoonShineUI::toast(message: 'Success',type: ToastType::SUCCESS,duration: 3000);
MoonShineUI::toast(message: 'Success',type: ToastType::SUCCESS,duration: 3000);
$type
- sets the color of the notification.$duration
- sets the duration for which the notification is displayed (milliseconds). By default, notifications are displayed for 2 seconds.
If you want the toast to remain visible until manually clicked:
MoonShineUI::toast(message: 'Success', duration: false);
MoonShineUI::toast(message: 'Success', duration: false);
MoonShineJsonResponse
MoonShineJsonResponse::make()->toast('Test', type: ToastType::SUCCESS, duration: 1000);
MoonShineJsonResponse::make()->toast('Test', type: ToastType::SUCCESS, duration: 1000);
The parameters used are the same as described above.
Events
ActionButton::make('Toast')->dispatchEvent(AlpineJs::event(JsEvent::TOAST, params: ToastEventParams::make(ToastType::SUCCESS, 'Hello', duration: 2000)));
ActionButton::make('Toast')->dispatchEvent(AlpineJs::event(JsEvent::TOAST, params: ToastEventParams::make(ToastType::SUCCESS, 'Hello', duration: 2000)));
The parameters used are the same as described above.
Duration
You can include a JavaScript snippet to globally override the toast display duration:
MoonShine.config().setToastDuration(5000);
MoonShine.config().setToastDuration(5000);