3

So my view users contains a list of all registered users. It also features a button to delete a specific user, so I created a controller and a route

users/delete/[email protected]

in my controller, UsersController, I handle the delete action. The user gets deleted and I return to users with an success alert which states that the user was deleted.

$message = "User ".$user. " successfully deleted";
return View::make('users')->with(compact('message')); 

which works, the alert is displayed. However, Chrome displays localhost:8000/users/delete/[email protected], which I can understand since I just 'make' (render) the users view within the users/delete/{email} route. Now that I'm really new to Laravel (and SO, so please be understanding) I wonder how to get Chrome to display localhost:8000/users and still show the message. I'm merely confused.

2
  • Obvioulsy you need to redirect to proper url after deleting a user. Commented Jul 1, 2017 at 19:59
  • 1
    Well, yes, but I still want to display the success alert and its message is passed to the view by $message. And thus, I don't know how to do this. Commented Jul 1, 2017 at 20:02

1 Answer 1

3

What you are looking for is called "flash messages". Here is an example:

On your controller you redirect with the message.

return redirect('users')->with('status', 'User deleted!');

After you simply have to display the message on the view like this.

@if (session('status'))
    <div class="alert alert-success">
        {{ session('status') }}
    </div>
@endif

Here is more documentation: https://laravel.com/docs/5.4/redirects#redirecting-with-flashed-session-data

Sign up to request clarification or add additional context in comments.

2 Comments

All right, this looks interesting. What if I wanted to pass an array through? Does it work without further ado?
Yes you can also use an array, that should work fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.