Advanced

Toasts

Flash

Toast notifications use the session mechanism (session()->flash()) and can be easily triggered directly from controllers.

use MoonShine\Support\Enums\ToastType;
 
toast(message: 'Hello');
use MoonShine\Support\Enums\ToastType;
 
toast(message: 'Hello');

You can also specify the notification type and duration:

toast(
message: 'Success',
type: ToastType::SUCCESS,
duration: 3000
);
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 notification not to disappear automatically, but to be removed only after the user clicks, set the $duration parameter to false.

toast(message: 'Success', duration: false);
toast(message: 'Success', duration: false);

JsonResponse

JsonResponse::make()
->toast('Test', type: ToastType::SUCCESS, duration: 1000);
JsonResponse::make()
->toast('Test', type: ToastType::SUCCESS, duration: 1000);

If you need to change toast notifications in ModelResource, see ModelResource > Response modifiers section.

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);