I would do this via ID rather than the email address (what if they change their email address)
This will get the related row, using "Auth" which has the current authenticated user
$userInfo = \App\UserInfo::where('user_id','=',\Auth::user()->id)
->first();
You can then change stuff and save it
$userInfo->city = 'London';
$userInfo->save();
You could also set up a one to one relation ship between the two objects (read the docs), although I would probably store all this info in the main user table, unless there is a good reason not to
First, thanks for the answer. But there's one problem. Ofcourse, I could also store all the information in the main user table, but still there's one problem. Auth::user()->id is null (I think this is because its automatically generated?). I'm not using a special column called 'user_id', its just the primary key, that I want to use as the ID for the user. However, after I got the information, I can simply call $userInfo->city = 'MyCity'; and $userInfo->save(); and its automatically saved into the correct column?
Edit: And another question: is \App\UserInfo always the way I can access this table?
Edit2: Actually it seems like I don't even have the file UserInfo, just the file "User.php", containing two vars $fillable and $hidden.
\Auth::user()->id will be populated after a user authenticates, it will be the primary key of your user table (assuming you are using the default primary key)
user_id would be in your user_info table and is the foreign key linking the user_info record to your user record
Yes once you have the models setup correctly you can do $userInfo->city, etc
Edit1: That is how you would access via eloquent, you can of course also access via query builder etc
Edit2: The user model ships with laravel, you would need to create the UserInfo model - if you store all the info in the User model you can skip this, and once you have the fields in the database you can just do $user->city = 'My city';
I would take a look at the following in the docs:
https://laravel.com/docs/5.3/authentication https://laravel.com/docs/5.3/eloquent
Got it now. Even if I'm still not sure about the eloquent Modelling things, but this is just pure reading. Thanks for the help and giving me some food for thought.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community