How to add an alert on Laravel 5?
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# How to Add a Success Alert After Deleting Data in Laravel
As developers, we often deal with operations where success or failure needs to be communicated clearly to the user. When you execute a destructive action like deleting a record, simply redirecting the user back is functional, but it leaves them wondering if the operation actually succeeded. A good user experience demands immediate feedback.
This post will walk you through the best, most idiomatic way to display an alert after successfully deleting data in your Laravel application, moving beyond simple redirects to provide meaningful feedback.
## The Problem with Basic Redirection
In your provided code snippet:
```php
public function hapus(Request $request, $id)
{
DB::table('kelompok')
->where('id', $id)
->delete();
return redirect()->back(); // This only redirects, no alert is shown.
}
```
While `return redirect()->back()` successfully sends the user back to the previous page, it doesn't inherently provide a visible notification that the deletion was successful. For critical actions, we need a mechanism to store this success message temporarily and display it on the next request.
## The Laravel Solution: Session Flashing
The most common and lightweight way to handle transient messages like success or error notifications in Laravel is by utilizing the Session flashing mechanism. This method ensures that the message is only displayed once, right after the redirect, making the flow clean and efficient.
### Step-by-Step Implementation
To implement a successful alert, you need to leverage the `session()` helper to flash data.
**1. Modify the Controller Method:**
After successfully performing the database operation (the deletion), you will store a success message in the session before redirecting.
```php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class GroupController extends Controller
{
public function hapus(Request $request, $id)
{
// 1. Perform the database operation (Deletion)
$deleted = DB::table('kelompok')
->where('id', $id)
->delete();
// Optional: Check if a row was actually deleted for better error handling
if ($deleted) {
// 2. Flash the success message to the session
session()->flash('success', 'Data berhasil dihapus!');
} else {
// Handle case where ID was not found
session()->flash('error', 'ID tidak ditemukan.');
}
// 3. Redirect back
return redirect()->back();
}
}
```
**2. Display the Alert in the View:**
Now, on the view that receives the redirect (usually the form page or the index page), you check for the existence of these flashed session variables and display them using Blade directives. This is where you create your popup alert.
In your Blade file:
```blade
{{-- Check for success message --}}
@if (session('success'))
{{ session('success') }}
@endif
{{-- Check for error message (for failed deletions) --}}
@if (session('error'))
{{ session('error') }}
@endif
{{-- Rest of your form or page content --}}