Laravel PHP Flash Message Functionality Integration

Laravel flash message is a way of showing success or error message after page redirection based on success or error. Tthe library php flasher is a popular one to integrate flash message functionality in laravel application. In whole tutorial we will show how to add this feature. 

Install PHPFlasher Composer Package

composer require php-flasher/flasher-laravel
php artisan flasher:install

Make a Controller

Create your own controller and use flash() helper to show flash message. You can use flash based on success or error

namespace App\Http\Controllers;

class BookController extends Controller
{
    public function saveBook()
    {
        //todo:: write your own logic here

        if (true) {

            flash()->success('Book successfully added');
        }

        return view('welcome');
    }


    public function updateBook()
    {
        //todo:: write your own logic here

        $status = false;
        if (!$status) {

            flash()->error('Failed to update your book data');
        }

        return view('welcome');

    }
}

Read also: Laravel collection and common collection methods

Read also: Laravel export collection to xlsx file download

Read also: Make middleware in laravel 11

Define Routes

use Illuminate\Support\Facades\Route;

Route::get('/save',[\App\Http\Controllers\BookController::class,'saveBook']);
Route::get('/update',[\App\Http\Controllers\BookController::class,'updateBook']);


Now access your defined routes using
http://127.0.0.1:8000/save
http://127.0.0.1:8000/update

Success flash message


Read also: Create shortcode in laravel same as wordpress

Read also: Laravel 11 sanctum api login registration


Error Flash Message


Tags: