Show message with Flash in Laravel 5 when redirecting

4

I am trying to display a message (Notification) when creating a user using Flash (Laracast).

In my user controller I have the store function:

public function store(Request $request){

    $user = new User($request->all());
    $user->password = bcrypt($request->password);
    $user->save();

    Flash::success("Se ha registrado el usuario ".$user->name." de manera exitosa!");

    return redirect()->route('admin.users.index');    
}

Then I added: @include('flash::message') in the body of my template.

When creating a user and executing the redirection, the message does not appear on the page /users (corresponding to the index)

If I use, return $this->index(); works perfectly for me but the URL does not change to /users as I expected.

I've tried putting a direct message in the controller index and it works fine. (That is not using redirection) Ex:

public function index(){
    $users = User::orderBy('id', 'DESC')->paginate(5);

    Flash::success("Mensaje de prueba");

    return view('admin.users.users')->with('users', $users);
}

Any help I could use.

    
asked by Jose Gratereaux 12.01.2016 в 04:16
source

1 answer

1

Remember that in Laravel 5, the flash messages are put to the session that makes the request, something kind:

//En tu controller
Session::flash('flash_message', 'Mensaje de prueba');
//aquí haces render o redirect a tu view

And in your view:

@if(Session::has('flash_message'))
{{Session::get('flash_message')}}
@endif

That way you can display your flash messages.

I recommend you visit laracasts, it is a great reference for those of us who are interested in the framework. The concrete video for this can be seen in the here .

Greetings.

    
answered by 12.01.2016 в 18:23