diff --git a/src/FlashMessageServiceProvider.php b/src/FlashMessageServiceProvider.php index e709bef..27c8261 100644 --- a/src/FlashMessageServiceProvider.php +++ b/src/FlashMessageServiceProvider.php @@ -4,6 +4,7 @@ use Bilfeldt\LaravelFlashMessage\View\Components\Alert; use Bilfeldt\LaravelFlashMessage\View\Components\AlertMessages; +use Illuminate\Http\RedirectResponse; use Illuminate\View\Factory; use Illuminate\View\View; use Spatie\LaravelPackageTools\Package; @@ -80,5 +81,22 @@ public function packageBooted() return $this; }); + + // This is used to add a message from a controller when returning a redirect: redirect()->withMessage($message) + RedirectResponse::macro('withMessage', function (Message $message, string $bag = 'default'): RedirectResponse { + session_message($message, $bag); + + return $this; + }); + + // This is used to add messages from a controller when returning a redirect: redirect()->withMessage([$message1, $message2]) + RedirectResponse::macro('withMessages', function (array $messages, string $bag = 'default'): RedirectResponse { + /** @var \Bilfeldt\LaravelFlashMessage\Message $message */ + foreach ($messages as $message) { + session_message($message, $bag); + } + + return $this; + }); } }