php - Laravel 5.1 Session not working outside Route::get -
i have code , working:
route::get('addnew',function(){ $user = users::where('username','=',session('username'))->first(); $data = $user->toarray(); return view('layout.addnew')->with($data); }); route::post('addnew', ['uses'=>'userscontroller@addnew']);
with code above: session('username') not null
but, when use code below:
$user = users::where('username','=',session('username'))->first(); $data = $user->toarray(); route::get('addnew',function() use($data){ return view('layout.addnew')->with($data); }); route::post('addnew', ['uses'=>'userscontroller@addnew']);
with code above: session('username') null => $data non-object , code not working.
somebody me, please!
thank much!
its better if this
routes.php
route::get('/addnew', 'homecontroller@addnew');
in controller (homecontroller in case should there default)
add controller
public function getuser(){ return users::where('username','=',session('username'))->first()->toarray(); } public function addnew() { return view('layout.addnew')->with('user',$this->getuser()); }