Hello Forks, using the Laravel success message example, we will teach you how to show success messages in Laravel 11 return from the controller to blade files. It’s common in web applications to provide feedback to users, informing them about successful actions or any encountered errors. We often utilize the return redirect back method from controller files to dynamically display such messages.
Showing a success message to the user view is most important and best because the user is satisfied when they have changed anything in the admin panel.
In Laravel, we can effectively show the success message in Laravel using various methods such as return view, return back(), and return redirect back(). By utilizing the session predefined method “with()” in the controller file and passing the message, we can seamlessly display it in the corresponding blade file. Let’s delve into an example code to understand how to send session messages from the controller and exhibit them in the blade file.
How to Show Success Message in Laravel 11
If you wish to display a success message on the same page, you can utilize the return redirect back or return back methods in Laravel.
return redirect()->back()->with('message', 'This is the success message.');
or
return back()->with('message', 'This is the success message.');
In your Blade file, you can implement the following logic: If there is a session message available, display it on the page.
@if(session()->has('message'))
<div class="alert alert-success">
{{ session()->get('message') }}
</div>
@endif
Controller Code with Route: If you want to show the success message a specific routes then you can redirect the file with the mssage just like in the controller file, we need to do just like this:
return redirect()->route('users.index')->with('message', 'The success message!');
Blade File Code: After the return from the controller we need to add the session message in blade file;
@if(session()->has('message'))
<div class="alert alert-success">
{{ session()->get('message') }}
</div>
@endif
If you’re still unclear, you can follow this comprehensive step-by-step example to learn how to display success messages in a Laravel application:
Step 1: Download Laravel App
First, you need to install a new laravel application using the following command:
composer create-project --prefer-dist laravel/laravel laravel-app
Next, go inside the app
cd laravel-app
Step 2: Update the Database Credentials
Next, go to the project root directory and open the .env file and update your database credentials just like below.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db name
DB_USERNAME=db user name
DB_PASSWORD=db password
Step 3: Run Migration
Next, we need to run the migrate command to generate database tables. So open your terminal and run the below command.
php artisan migrate
Step 4: Make Routes
Here, we’ll add three routes to demonstrate how to show a success message example in a Laravel application.
So, open the routes/web.php file and update the below routes on it;
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\UserController; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('users', [UserController::class, 'index'])->name('users.index'); Route::get('users/create', [UserController::class, 'create'])->name('users.create'); Route::post('users', [UserController::class, 'store'])->name('users.store');
Step 5: Create a User Controller
Next, we will create a new controller using the following command:
php artisan make:controller UserController
After this command a new controller will be generated in app/http/controllers directory.
So, open the app/Http/Controllers/UserController.php file and update the following code on it;
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; class UserController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index(Request $request) { $users = User::paginate(20); return view('users', compact('users')); } /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function create(Request $request) { return view('create'); } /** * Write code on Method * * @return response() */ public function store($id) { // add logic here return redirect()->route('users.index')->with('message', 'User created successfully!'); } }
Step 6: Create Blade Files
Now, we will create two blade files one to show the form and another will show our list with a success message code. So when users submit the form they will redirect to the list page with the success message.
So, create a new blade users.blade.php file inside the resources/views directory. Next, open the resources/views/users.blade.php file and add the below code on it;
<!DOCTYPE html>
<html>
<head>
<title>Laravel App</title>
<meta name="csrf-token" content="{{ csrf_token() }}">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Laravel success message example </h2>
</div>
<div class="pull-right">
<a href="{{ route('users.create') }}" class="btn btn-primary">Create</a>
</div>
</div>
</div>
@if(session()->has('message'))
<div class="alert alert-success">
{{ session()->get('message') }}
</div>
@endif
<table class="table table-bordered data-table">
<thead>
<tr>
<th>No</th>
<th>Name</th>
<th>Email</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</body>
</html>
Again navigate to resources/views directory and create another new file create.blade.php file. Next, open the resources/views/create.blade.php file and add the below code on it;
<!DOCTYPE html>
<html>
<head>
<title>Laravel App</title>
<meta name="csrf-token" content="{{ csrf_token() }}">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Add New User</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('posts.index') }}"> Back</a>
</div>
</div>
</div>
<form action="{{ route('users.store') }}" method="POST">
@csrf
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
<input type="text" name="title" class="form-control" placeholder="Title">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
</div>
</body>
</html>
Step 7: Run Application and Test
Now, its time to test our implemented laravel showing success message example; So open the terminal and start your application using the serve command;
php artisan serve
Next, hit the below URL in the browser for test.
localhost:8000/users
I hope you now have a clear understanding of how to implement success messages in a Laravel application. If you have any further questions or queries, please feel free to reach out to us through the comment section. We’re here to assist you!