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

0

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

I have tried it many times with flash sessions and there is no human way to show the message.

I am using restful controllers, and in the store method it is where I try to redirect to the view, as I say I have tried with the method redirect :: to in this way

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

but he does not show me anything.

I have tried returning a view

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

but being restful drivers it takes me to a route that is not correct, since the create method and the store method share the same route (I do not know if this can be changed somewhere) finally I tried with

Redirect::route('raulPalacio')

and

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

and in both cases it gives me route not defined. So I wonder if there is any way to send an array and redirect at the same time using restful drivers and that it works.

    
asked by KurodoAkabane 30.03.2016 в 11:44
source

2 answers

3

Well I show my messages in the following way

First in the controller we call Session and Redirect

Use Session
Use Redirect

On 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 that must be something like this ...

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

You must put it in a middleware group.

    
answered by 06.06.2016 в 16:40
1

In the method of your controller after doing your insertion 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
);
    
answered by 02.04.2016 в 09:51