Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
In this tutorial, we will learn How to use Session Flash Message in Laravel 8 application. As we know very well Flash Message is a basic feature of every web application. if you store, update, and remove an item, post, product, category, etc. So we should always prefer to message or alert after adding, updating, and removing items in laravel 5, laravel 6, laravel 7, and laravel 8 app.
First, what we’re doing here, This is the example :
If we get any error or success and we perform an action then flash message helps us to know it.
There are multiple ways to show a Custom Flash Message for error and success actions, We can use sweet alert, third-party jQuery plugins, and more, I recommend you to use Session Flash Message in Laravel 8.
In this example, we will use the default laravel Session, because it is fast and reliable to use.
routes\web.php :
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\FlashmsgExampleController;
#Flash Message Example
Route::get('/add-category', [FlashmsgExampleController::class, 'addcategory']);
Route::get('/category-list', [FlashmsgExampleController::class, 'listcategory'])->name('cat.list');
Route::post('/category-store', [FlashmsgExampleController::class, 'storecategory'])->name('cat.store');
Http\Controllers\FlashmsgExampleController.php :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Category;
class FlashmsgExampleController extends Controller
{
public function addcategory()
{
return view('flashmsgexample.add');
}
public function listcategory()
{
$category = Category::get();
return view('flashmsgexample.list',compact('category'));
}
public function storecategory(Request $request)
{
$cat = new Category();
$cat->name = $request->cat_name;
$cat->description = $request->cat_description;
$cat->save();
return redirect()->route('cat.list')->with('flash_msg_success','Category added Successfully !');
}
}
resources\views\flashmsgexample\add.blade.php
<div class="container">
<div class="row justify-content-center">
<div class="col-lg-6">
<div class="main">
<h3><a>Flash Session Message (Success & Error) Example | Laravel</a></h3>
<form role="form" action="{{route('cat.store')}}" method="post">
@csrf
<div class="form-group">
<label for="name">Category Name <span class="text-danger">*</span></label>
<input type="text" name="cat_name" class="form-control">
</div>
<div class="form-group">
<label for="description not ">Category Description <span class="text-danger">*</span></label>
<input type="text" name="cat_description" class="form-control">
</div>
<div class="form-group">
<button type="submit" class="btn btn btn-secondary">
save
</button>
</form>
</div>
</div>
</div>
</div>
resources\views\flashmsgexample\list.blade.php
<div class="container">
<h3>Flash Session Message (Success & Error) Example | Laravel</h3>
<br>
<div class="row justify-content-end mb-1">
<div class="col-sm-4 float-right">
@if(Session::has('flash_msg_success'))
<div class="alert alert-success alert-dismissible p-2">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Success!</strong> {!! session('flash_msg_success')!!}.
</div>
@endif
</div>
</div>
<table class="table">
<thead>
<tr>
<th>S.no</th>
<th>Category name</th>
<th>Category Description</th>
</tr>
</thead>
<tbody>
@foreach($category as $key => $data)
<tr>
<td>{{ $key+1 }}</td>
<td>{{ $data->name }}</td>
<td>{{ $data->description }}</td>
</tr>
</tbody>
@endforeach
</table>
</div>
Also Read: Sweet alert delete confirm in Laravel 8.