How to show Save success message in Laravel 5.1?

How to show Save success message in Laravel 5.1?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Displaying Save Success Messages in Laravel 5.1

In Laravel 5.1, persisting data is straightforward thanks to Eloquent and the request validation middleware. However, telling the user that their action succeeded requires a deliberate step that many developers overlook: sending flash data. Your current controller method successfully validates the input against rules like required|unique:seeders and hydrates the Seeders model before redirecting back to the index route. The problem is that this redirect happens without any context for the user, leaving them wondering if the operation actually completed.

To display a success message after a redirect() call, you must use the session flash bag. In Laravel 5.1, this mechanism lives within the request object itself. You don't need to touch the session facade directly; the framework handles the retrieval and storage of flash data automatically. The standard pattern involves calling withInput() (to re-populate the form) and then adding a message via withErrors() or withSuccess().

Here is how you modify your store method:

public function store(Request $request)
{
    $this->validate($request, [
        'name' => 'required|unique:seeders|max:255',
        'address' => 'required',
        'age'  => 'required',
    ]);

    $seeder = new Seeders();
    $seeder->name = $request->input('name');
    $seeder->address = $request->input('address');
    $seeder->age = $request->input('age');
    $seeder->save();

    // Flash the success message and return redirect
    return redirect()->route('photo.index')
        ->withSuccess('Successfully created new seeders record.');
}

The ->withSuccess() method attaches a key-value pair to the session. When you redirect, Laravel stores this data temporarily. The next time the user visits the destination route (in this case, photo.index), Laravel checks for flash messages in the request object and passes them to your view automatically.

You can then retrieve this message in your Blade template using the {{ $message }} syntax or the session() helper. A common approach is to check if a specific variable exists before rendering it:

{{-- resources/views/photo/index.blade.php --}}
@if(session('success'))
    <div class="alert alert-success">
        {{ session('success') }}
    </div>
@endif

<table>
    <!-- Your existing table data -->
</table>

This pattern ensures the message appears only once, on the immediate next request, and then vanishes. This behavior is crucial for user experience; if you didn't clear it or handle it correctly, the "Success" toast might appear every time the user views their seeders list, which would be misleading.

Trade-offs and Best Practices

While withSuccess() is convenient, it has limitations compared to modern Laravel versions (5.4+). In later versions, you can use Flash::success() from the laravel-notification-channel package or simply rely on the built-in $message variable if using Blade's flash bag helpers directly. However, in Laravel 5.1, sticking to the withSuccess chain is the most reliable method without adding extra dependencies.

Another consideration is form re-population. If you want the user to see their entered data after a successful save (perhaps they are filling out a long form), you must chain ->withInput() before returning the redirect. Without it, the form fields will clear, and the user might think they need to re-enter everything.

return redirect()->route('photo.index')
    ->withSuccess('Successfully created new seeders record.')
    ->withInput(); // Optional: keeps form populated

Using withErrors() instead of withSuccess() is equally valid but requires a different Blade syntax, typically checking $errors->any(). Since your validation passed and the record saved successfully, withSuccess remains the cleaner semantic choice here.

If you are maintaining legacy Laravel 5.1 codebases, understanding these flash data mechanics is essential for providing immediate feedback without relying on complex AJAX callbacks or page reloads that might feel clunky to the user.

Our network