How to show message after filter in laravel?

by filiberto , in category: PHP Frameworks , 3 months ago

How to show message after filter in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by darion , 3 months ago

@filiberto 

To show a message after filtering in Laravel, you can follow these steps:

  1. In your controller method where you perform the filter, set a flash message using the session() helper function. For example:
1
2
3
4
5
6
7
8
public function filterData(Request $request)
{
    // Your filter logic goes here

    session()->flash('message', 'Filter applied successfully.');

    return redirect()->route('your.route.name');
}


  1. In your blade view file where you want to display the message, use the @if directive to check if the message key exists in the session. If it exists, display the message using appropriate HTML markup. For example:
1
2
3
4
5
6
<!-- Your blade view file -->
@if (session('message'))
    <div class="alert alert-success">
        {{ session('message') }}
    </div>
@endif


This code checks if the message key exists in the session and if it does, it displays an alert div with the message content. The session('message') retrieves the value of the message key from the session.

  1. Make sure you have included the necessary CSS classes or styles to style the alert div as per your design requirements.


With these steps, after applying the filter, a success message will be shown to the user on the redirected page.