In this article, I am going to explain the laravel 7 flash message example. This article goes in detailed on flash message in controller laravel 7. I would like to show you how to display flash message in laravel 7 after login or post data in database.
Flash notifications are required in laravel 7 application because that way we can give alter with what progress complete, error, warning etc. In this tutorial, I added several ways to give a flash message like redirect with success message, redirect with an error message, redirect with a warning message and redirect with info message. In this example, we use a bootstrap flash alert layout that way it becomes good layout.
Read Also: Laravel 7 Email Verification
Let’s get started with create flash message laravel 7.
There we will define many types of flash message notification like alert success, alert danger, alert info, alert warning messages in bootstrap laravel 7 projects and display these according to task like on success display success message when gettting any error then dispaly error message etc.
So let’s follow below step:
Step 1: Create Global File For Flash Message
we will create new blade file flash-message.blade.php and write code for messages like success,error,warning,info and validation errors etc.
resources/views/flash-message.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | @if ($message = Session::get('success')) <div class="alert alert-success alert-block"> <button type="button" class="close" data-dismiss="alert">×</button> <strong>{{ $message }}</strong> </div> @endif @if ($message = Session::get('error')) <div class="alert alert-danger alert-block"> <button type="button" class="close" data-dismiss="alert">×</button> <strong>{{ $message }}</strong> </div> @endif @if ($message = Session::get('warning')) <div class="alert alert-warning alert-block"> <button type="button" class="close" data-dismiss="alert">×</button> <strong>{{ $message }}</strong> </div> @endif @if ($message = Session::get('info')) <div class="alert alert-info alert-block"> <button type="button" class="close" data-dismiss="alert">×</button> <strong>{{ $message }}</strong> </div> @endif @if ($errors->any()) <div class="alert alert-danger"> <button type="button" class="close" data-dismiss="alert">×</button> Please check the form below for errors </div> @endif |
Read Also: Integrate PayUmoney Payment Gateway
Step 2: Use Flash Message file in app.blade.php file like as bellow:
1 2 3 | @include('flash-message') |
Step 3: Use Flash Messages with Redirect
In this step I will be show you how to redirect with message like error, success etc.
1. Redirect with success message
We can redirect route or redirect URL or redirect back with success flash message, we can use in controller like this way:
1 2 3 4 5 6 7 8 9 10 11 12 13 | public function create(Request $request) { $this->validate($request,[ 'title' => 'required', 'details' => 'required' ]); $post = Post::create($request->all()); return back()->with('success','Post created successfully!'); } |
2. Redirect with error message
We can redirect route or redirect URL or redirect back with error flash message, we can use in controller like this way:
1 2 3 4 5 6 7 | public function create(Request $request) { return redirect()->route('home') ->with('error','You have no permission for this page!'); } |
Read Also: Log All SQL Queries In Laravel
3. Redirect with warning message
We can simple redirect route or redirect URL or redirect back with warning flash message, we can use in controller like this way:
1 2 3 4 5 6 7 | public function create(Request $request) { return redirect()->route('home') ->with('warning',"You can't Open this link"); } |
4. Redirect with info message
We can redirect route or redirect URL or redirect back with info flash message, we can use in controller like this way:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public function create(Request $request) { $this->validate($request,[ 'title' => 'required', 'details' => 'required' ]); $post = Post::create($request->all()); return back()->with('info','You have added new post!'); } |
I hope you found your best…
I’m a full-stack developer. My hobby and profession to write blog and programming tips that helps to others. I am a great admirer of PHP, Laravel, Codeigniter, AngularJS, Vue.js, Javascript, JQuery, WordPress, Plugin Development, Theme Development and Bootstrap from the early stage.
Leave a Reply