Skip to content

Commit

Permalink
Fix issue that messages were not available when adding them to a redi…
Browse files Browse the repository at this point in the history
…rect
  • Loading branch information
bilfeldt committed Mar 28, 2022
1 parent e5e6f98 commit 17ae569
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/FlashMessageServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
});
}
}

0 comments on commit 17ae569

Please sign in to comment.