How to keep data in my session during all requests in Laravel 5.1?

2

Greetings, what I need is to keep some data sent from a form in an array that I store in Session de laravel, I'm doing it in the following way:

In the routes.php I have:

Route::bind('evento', function($slug){
    return Deportes\Evento::where('slug', $slug)->first();
});



Route::get('tienda/agregar/{evento}', [
    'as' => 'inscribir-evento',
    'uses' => '[email protected]']);


Route::get('tienda/{slug}', [ 
    'as' => 'eventos-detalles',
    'uses' => '[email protected]']);


Route::post('tienda/inscribir/{evento}',[
       'as'=> 'inscrito-inscribir',
       'uses'=>'[email protected]'
    ]);
Route::get('tienda/', '[email protected]');

On my Controller:

public function index()
    {
       // $tienda = \Session::get('tienda');
        dd(\Session::get('tienda'));
       // return view('tienda.tienda', compact('tienda'));
    }

public function agregar(Evento $evento){
       
        $tienda = \Session::get('tienda');
        if ($tienda = null) {
            $tienda[$evento->slug] = $evento;
            \Session::put('tienda', $tienda);
            return view('tienda.inscribir', compact('tienda'));
        }
        else{
                 
            \Session::forget('tienda');
             $tienda[$evento->slug] = $evento;
            \Session::put('tienda', $tienda);
            return view('tienda.inscribir', compact('tienda'));
        }
        
        

    }
    
    public function inscribir(Evento $evento, Request $request){
        $tienda = \Session::get('tienda');
        $inscrito = [            
            'nombre'=>$request->get('nombre'),            
            'edad'=>$request->get('edad'),
            'cedula'=>$request->get('cedula'),
            ];
        $tienda[$inscrito['cedula']] = $inscrito;
        \Session::put('tienda', $tienda);
        dd(\Session::get('tienda'));
        //return view('tienda.inscribir', compact('tienda'));
    
        
    }

Now, when I register the person in the event, I add the person to the array by his / her cedula but when I go to see if it stays in the array somewhere else, the person does not appear ...

In fact what I need is to add several participants to the event ... the event is already in database, but the registered ones I want to have them temporarily during the whole session, until the conditions are fulfilled as it could be done?

    
asked by Susje 07.07.2017 в 13:27
source

1 answer

1

Well, I really did a test and the session works without problem, what happens is that you are using the helper dd() and you are not allowing the modification of the session to actually be stored, which occurs at the end of the cycle Life of Laravel.

You have two options:

  • Remove the dd() and try with dump() in the next view you show.
  • Use Session::save() after Session::put() to store session data immediately, although this would be in specific cases or to test.
answered by 07.07.2017 / 17:46
source