Get Appointment

Blog Single

How to show Laravel session success message

  • Vfix Technology
  • 24 Dec 2023
  • Laravel
  • 473 Views

Introduction

User feedback is essential for creating a seamless and intuitive web experience. Laravel, a popular PHP framework, provides a robust session management system that enables developers to store and retrieve user-specific data across multiple requests. Leveraging Laravel's session functionality, you can implement inline messages to provide immediate feedback to users about their actions or application status. In this blog post, we will explore how to display inline messages in Laravel sessions, enhancing user interaction and improving the overall user experience.

Understanding Inline Messages in Laravel Sessions:

Inline messages, also known as flash messages, are concise notifications that appear temporarily within the user interface to provide feedback on specific actions or application status. These messages play a vital role in guiding users, confirming successful operations, alerting about errors, or conveying important information. By utilizing Laravel's session management system, you can easily implement inline messages to deliver real-time feedback to your users.

Here to show the success message I am using the example of the create the data in database and after the data has been created you will get success message.

Step 1: Create the backend

public function store(Request $request)
    {
        $data = $request->validate([
            'name'     => 'required',
            'email'      => 'required',
        ]);


        $user = new User;
        $user->name= $request->name;
        $user->email= $request->email;

        $user->save();
        return redirect()->route('user.index')->with('success','User has been added successfully!');
    }

over here we have added the user and then we have returned back to the user index with success message here we have given the command that after creating the user come with the success message

Step 2: Featch the succes message on the frontend

@if (session()->has('success'))
    <div class="alert alert-dismissable alert-success">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
            <span aria-hidden="true">&times;</span>
         </button>
         <strong>
             {!! session()->get('success') !!}
         </strong>
     </div>
@endif

Here we have used the bootstrap alert to show the success message you can use your own customized code. We have give  the command that if there is any success message in the session show this alert message of the success

Step 3: Auto hide alert

After the message has been appered we want it to be hide after some time automatically to enhance the user exprience

<script>
    $(document).ready(function() {
        $(".alert").delay(6000).slideUp(300);
     });
</script>

This jQuery code will hide the message after six seconds you can increase of descrease this according to your need.

To show errors read this How to show laravel session errors.

Share :


+91 8447 525 204 Request Estimate