Redireccionamiento de Laravel hacia atrás con mensaje ()

Resuelto M T asked hace 10 años • 25 respuestas

Estoy intentando redirigir a la página anterior con un mensaje cuando hay un error fatal.

App::fatal(function($exception)
{
    return Redirect::back()->with('msg', 'The Message');
}

En la vista intentando acceder al mensaje con

Sessions::get('msg')

Pero no se procesa nada, ¿estoy haciendo algo mal aquí?

M T avatar Nov 07 '13 21:11 M T
Aceptado

Intentar

return Redirect::back()->withErrors(['msg' => 'The Message']);

y dentro de tu vista llama a esto

@if($errors->any())
<h4>{{$errors->first()}}</h4>
@endif
giannis christofakis avatar Nov 07 '2013 14:11 giannis christofakis

Laravel 5 y posteriores

Controlador

 return redirect()->back()->with('success', 'your message,here');   

Cuchilla:

@if (\Session::has('success'))
    <div class="alert alert-success">
        <ul>
            <li>{!! \Session::get('success') !!}</li>
        </ul>
    </div>
@endif
Ketan Akbari avatar Apr 19 '2016 05:04 Ketan Akbari

El enfoque alternativo sería

Controlador

use Session;
       
Session::flash('message', "Special message goes here");
return Redirect::back();

Vista

@if (Session::has('message'))
   <div class="alert alert-info">{{ Session::get('message') }}</div>
@endif
Rick avatar May 21 '2014 21:05 Rick

En Laravel 5.4 lo siguiente funcionó para mí:

return back()->withErrors(['field_name' => ['Your custom message here.']]);
haakym avatar May 03 '2017 13:05 haakym