Display an insert message without flash sessions in laravel 5.2 with restfull drivers

I'm learning Laravel and I want to do an insert in a table, and that when I insert it it shows a message in the view like that the record has been inserted.

I have tried it countless times with flash sessions and there is no human way for it to display the message.

I'm using restful controllers, and in the store method it's where I try to redirect to the view, as I say I've tried with the redirect:: to method thus

Redirect::to('raulpalaciox/')->with('message', 'enviado');

But it shows me nothing.

I have tried returning a view

return view('raulPalacioview.usuario.index',compact('mensajex'));

But being restful controllers takes me to a path that is not correct, since the create method and the store method share the same path.(I don't know if you can change this somewhere) finally I have tried with

Redirect::route('raulPalacio')

And

Redirect::route('raulPalacio',array('mensaje'=>'enviado')

And in both cases it puts me route not defined. So I'm wondering if there is any way to send an array and redirect at once using restful drivers and make it work.

 0
Author: Shaz, 2016-03-30

3 answers

Well I show my messages as follows

First in the controller we invoke Session and Redirect

Use Session
Use Redirect

In your controller either in the store, destroy or update function

Session::flash('message','Your message');
return Redirect::to('/yourroute');

Then in your view, you show the message you defined in your controller

@if(Session::has('message'))
<div class="alert alert-success alert-dismissible" role="alert">
  <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
  {{Session::get('message')}}
</div>
@endif

Finally, your route must be something like this...

Route::resource('ruta','MyController') 

You must put it in a middleware group.

 4
Author: Elio, 2016-06-06 15:32:09

In your controller method after doing your insert process you can respond to your view using json, for example:

try {
    // lógica para hacer la inserción
    return response()->json(array('status' => 'ok', 'code'=>200, 'message'=>'El registro ha sido guardado'));
} catch(Exception $e) {
    return response()->json(array('status' => 'error', 'code'=>400, 'message'=>$e->getMessage())); //$e->getMessage() sólo para versión en desarrollo, puede cambiarse después por algo como 'Un error ha ocurrido'
}

And from your view make an AJAX call

$('#idBoton').on('click', function(e){
    e.preventDefault();
    var request = $.ajax({
            url: "{{ route('ruta') }}",
            type: 'POST',
            // otras parametros
        });
    request.done(function(response){
        console.log(response.message);//imprime en consola el resultado
    };
    // si ocurrió un error en el controlador imprimir la excepcion para depurarla
    request.fail(function(jqXHR, textStatus, errorThrown){
        console.log(jqXHR.responseText);// por ejemplo aquí se muestra la excepcion completa como si se tratará de una vista html
);
 1
Author: Agus Rdz, 2016-04-02 07:51:03

Inserting new record into the table works fine, but it doesn't show me the message. Any solutions?

   public function store(Request $request)
    {
        $this->validate($request,[ 'nombre'=>'required', 'resumen'=>'required', 'npagina'=>'required', 'edicion'=>'required', 'autor'=>'required', 'npagina'=>'required', 'precio'=>'required']);

        Libro::create($request->all());
        return redirect()->route('libros')->with('success','Registro creado satisfactoriamente');
    }
 0
Author: Benito El Marchoso, 2020-03-25 01:46:54