Skip to content

Commit

Permalink
Merge pull request #8 from bilfeldt/fix/redirect-with-messages
Browse files Browse the repository at this point in the history
Fix missing messages were doing redirect
  • Loading branch information
bilfeldt committed Mar 28, 2022
2 parents e5e6f98 + 17ae569 commit 9cefcbd
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/FlashMessageServiceProvider.php
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 9cefcbd

Please sign in to comment.