Best way to pass bootstrap alert / flash messages in Laravel

bootstrap alert in Laravel

In a web application, you often need to pass the status message variables across the pages to show the result of an action performed by the user.

If you are using bootstrap the best way you can show the status messages is to wrap them around an HTML element with a class of alert-success, alert-danger, alert-info and similar others ( Bootstrap Alerts )

Here is the custom code to display your status messages in the Laravel blade file.


@if(session()->has('message'))
            <div class="alert {{session('alert') ?? 'alert-info'}}">
                {{ session('message') }}
            </div>
 @endif

With this, you need to send two variables, one message which contains the actual message which you want to show the user on-screen and another alert type, If the second variable is skipped it will consider the default value of alert-info for the message.

Livewire Component Library

Now to send the messages on redirect you can do this.

Get special discount by using coupon code 5Balloons for hosting your Laravel / Wordpress websites on Digital Ocean using Cloudways. Follow this Cloudways Affiliate URL to apply the discount code and get the offer

Redirect back with danger message

return redirect()->back()->with(['message' => 'Missing validation', 'alert' => 'alert-danger']);

Redirect back with success message

return redirect()->back()->with(['message' => 'Record is saved into the database', 'alert' => 'alert-success']);

Redirect back with default alert type (will use alter-info by default)

return redirect()->back()->with(['message' => 'Great News']);

and so on…

Site Footer