Laravel 8 session flash message

Using Laravel session helper to display flash message

Posted by Ervin Adams on June 12, 2021

Show message on success or error using Laravel session 

In this example you will be shown how to use session helper to create success messages.

A user will be inserted and if successful, a flash message will be displayed.  

You can also use session facade to achieve the same results.

Ensure that your Laravel project is connected to a live database. Go to this link if you want to learn how to set up Laravel on your PC.

Step 1. Create a user controller by executing command.

php artisan make:controller UserController

go to "app/Http/Controllers" and edit the "UserController"

namespace App\Http\Controllers;

use App\Models\User;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\Hash;

class UserController extends Controller {

public function index(){

return view('index');


public function createUser(Request $request){

$name = $request->input('name');

$email = $request->input('email');

$password = Hash::make($request->input('password'));

//best practice is to create a repository for models

User::create(['name' => $name, 'email' => $email, 'password' => $password]);

//this is where the flash message is created using session helper.

session()->flash('message', 'User created successfully.');

return redirect('/');



Step 2. add routes to web.phpgo to "routes" and edit "web.php"

Step 3. Create a view go to "resources/views" and create "index.blade.php"

<!DOCTYPE html>


<meta charset="utf-8">

<title>Wecode101 flash message</title>

<meta name="description" content="Wecode101 flash message Laravel 8">

<meta name="author" content="Wecode101">

<meta name="csrf-token" content="{{ csrf_token() }}">

<link rel="stylesheet" href="" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">




<div class="container align-items-start">

<h3 class="d-flex justify-content-center">Create User</h3>

@if (session()->has('message'))

<div class="alert alert-success"> {{ session('message') }} </div>


<div class="container-fluid d-flex justify-content-center mt-4" style="max-width: 30em">

<form id="signin" class="form-horizontal validate-form" method="POST" action="{{ route('create.user') }}">

{{ csrf_field() }}

<div class="form-group">

<label for="name" class="control-label">Name</label>

<input id="name" type="text" class="form-control" name="name" value="{{ old('name') }}"/>


<div class="form-group">

<label for="email" class="control-label">Email</label>

<input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}"/>


<div class="form-group mb-2">

<label for="password" class="control-label">Password</label>

<input id="password" type="password" class="form-control" name="password" value="{{ old('password') }}"/>


<div class="form-group mb-0">

<button type="submit" class="btn btn-primary">submit</button>







Step 4. Start server

php artisan serve