Created
May 2, 2026 14:00
-
-
Save ghabriel25/6f33b8f8d8ea7306681761267c1d9ea4 to your computer and use it in GitHub Desktop.
Flux toast notification
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Traits\Livewire; | |
| trait WithSessionKey | |
| { | |
| public const string TOAST_SUCCESS = 'flux:success'; | |
| public const string TOAST_INFO = 'flux:info'; | |
| public const string TOAST_WARNING = 'flux:warning'; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Traits\Livewire; | |
| use Flux\Flux; | |
| use Illuminate\Support\Facades\Session; | |
| use Illuminate\Validation\ValidationException; | |
| trait WithToastNotification | |
| { | |
| use WithSessionKey; | |
| // Lifecycle Hooks | |
| public function renderedWithToastNotification(): void | |
| { | |
| $this->checkToastNotifications(); | |
| } | |
| protected function checkToastNotifications(): void | |
| { | |
| if (Session::has(self::TOAST_SUCCESS)) { | |
| $this->successToast(Session::pull(self::TOAST_SUCCESS)); | |
| } | |
| if (Session::has(self::TOAST_INFO)) { | |
| $this->infoToast(Session::pull(self::TOAST_INFO)); | |
| } | |
| if (Session::has(self::TOAST_WARNING)) { | |
| $this->warningToast(Session::pull(self::TOAST_WARNING)); | |
| } | |
| } | |
| // Lifecycle Hooks | |
| public function exceptionWithToastNotification($e, $stopPropagation): void | |
| { | |
| if ($e instanceof ValidationException) { | |
| $this->handleValidationException(); | |
| } | |
| $stopPropagation(); | |
| } | |
| protected function handleValidationException(): void | |
| { | |
| $errors = array_unique($this->getErrorBag()->all()); | |
| foreach ($errors as $error) { | |
| $this->errorToast($error); | |
| } | |
| } | |
| protected function successToast(string $message = ''): void | |
| { | |
| $this->fluxToast(message: $message, variant: 'success'); | |
| } | |
| protected function errorToast(string $message = ''): void | |
| { | |
| $this->fluxToast(message: $message, variant: 'danger'); | |
| } | |
| protected function infoToast(string $message = ''): void | |
| { | |
| $this->fluxToast(message: $message, variant: 'info'); | |
| } | |
| protected function warningToast(string $message = ''): void | |
| { | |
| $this->fluxToast(message: $message, variant: 'warning'); | |
| } | |
| protected function fluxToast($message, $variant, $heading = '', $duration = 3000): void | |
| { | |
| Flux::toast(text: $message, heading: $heading, duration: $duration, variant: $variant); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment