PHP Question

Laravel 5. How I can save and take back values of Session

I try save after log in of user, values in session database. I try use this code to validate login and put in session.

protected function login(Request $Request){
$var = $Request->session()->all();
return dd($var);

$user = Model\Account::where('username',$Request['username'])->first();
if(isset($user)){
if (Hash::check($Request['password'], $user->password))
{
$Request->Session()->put('usuarios',$user);
return view('aluno/showActivities');
}
else{
return dd('Invalid password.');
}
}
else{
return dd('User doesn't exists.');
}
}


OBS: I use a controller for execute login. This Route are defined like code in down.



Route::post('/new/login',['as' => 'authRegister', 'uses' => 'UserController@login',function () {
return Response::json(['Fail' => true], 404);
}] );





After login validation, the laravel did save this line in database.

Image of database line

He doesn't save user_id. How i can save this information?
Other problem is how i can take back the informations of session ? In method get I tried use $Request->session()->get('usuarios'), but returned null.

var_dump($user) =



yes. See the result.

object(App\Model\Account)#198 (23) { ["table":protected]=> string(7) "account" ["connection":protected]=> string(5) "mysql" ["fillable":protected]=> array(3) { [0]=> string(5) "email" [1]=> string(8) "password" [2]=> string(8) "username" } ["primaryKey":protected]=> string(2) "id" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true) ["attributes":protected]=> array(8) { ["id"]=> int(4) ["email"]=> string(20) "thiagothgb@gmail.com" ["password"]=> string(60) "$2y$10$yFZDFHH2tr6OeoRtEShSmOsX99TQ7e.o5Tb2mJ7sA4tNZEXr4s3tG" ["username"]=> string(10) "thiagothgb" ["deleted_at"]=> NULL ["created_at"]=> string(19) "2016-04-23 17:20:10" ["updated_at"]=> string(19) "2016-04-23 17:20:10" ["user_id"]=> int(6) } ["original":protected]=> array(8) { ["id"]=> int(4) ["email"]=> string(20) "thiagothgb@gmail.com" ["password"]=> string(60) "$2y$10$yFZDFHH2tr6OeoRtEShSmOsX99TQ7e.o5Tb2mJ7sA4tNZEXr4s3tG" ["username"]=> string(10) "thiagothgb" ["deleted_at"]=> NULL ["created_at"]=> string(19) "2016-04-23 17:20:10" ["updated_at"]=> string(19) "2016-04-23 17:20:10" ["user_id"]=> int(6) } ["relations":protected]=> array(0) { } ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["casts":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["with":protected]=> array(0) { } ["morphClass":protected]=> NULL ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) }




Answer

try this, and check whether you get the session values or not.

$user = Model\Account::where('username',$Request['username'])->first();
$Request->Session()->put('usuarios',$user->toArray());
echo "<pre>";
 print_r($Request->Session()->get('usuarios'));
echo "</pre>";
die();

show me your results