How to create an alert in a Laravel application using prologuephp alerts package

  • It raises a quick notice to the user.
  • can be customized
  • makes the alert messages more presentable and readable
composer require prologue/alerts
'Prologue\Alerts\AlertsServiceProvider', 
'Alert' => 'Prologue\Alerts\Facades\Alert',
php artisan vendor:publish - -provider= "Prologue\Alerts\AlertsServiceProvider"
  • Adding Alerts
Alert::info('This is an info message in a Laravel application.');
  • Update the store method to make use of the info method
Alert::info('This is the info instance message after creating a blog post')- >flash(); return redirect()- >route('posts.create')- >with('alerts', Alert::all());
Alert::error('Whoops, something has gone wrong!!!');
// Add some alerts and flash them to the session.
Alert::success('You have successfully logged in')->flash();
// Display the alerts in the admin dashboard view.
return View::make('dashboard')->with('alerts', Alert::all());
@foreach (Alert::all() as $alert)
{{ $alert }}
@endforeach
@if (Alert::has('success'))
{{ Alert::first('success') }}
@endif
  • Update the store method
Alert::success('You have successfully published a blog post')->flash();return redirect()->route('posts.create')->with('alerts', Alert::all());
  • Update the create.blade.php view file
@if (Alert::has('success'))
@foreach (Alert::all() as $alert)
<div class="alert alert-success">
<p>{{ $alert }}</p>
</div>
@endforeach
@endif
@foreach (Alert::getMessages() as $type => $messages)
@foreach ($messages as $message)
<div class="alert alert-{{ $type }}">{{ $message }}</div>
@endforeach
@endforeach
Alert::add('primary', 'You have successfully published a blog post')->flash(); 
  • Checking For Alerts
Alert::has(); // Will check for any alerts
Alert::has('error'); // Will check for any alerts listed as errors.
Alert::count(); // Will give you a total count of all alerts based on all levels within your alerts config.
Alert::count('error'); // Will tell you only the amount of errors and exclude any levels.
  • Credits

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store