Support the ongoing development of Laravel.io →
Authentication Database Eloquent

I'm using Laravel for a little app. In this app I added the auth things of Laravel (verison 5.3), so I automatically got a possibility to register and log in.

So what I got now are a few controllers, 2 vies and a table users, with name, email and password and a unique auto incrementing primary key. What I want now, is to make the logged in user able to add additional information, like City, Street, Telephone, etc..

I don't want to make the user able to add this directly at register. What I want instead, is, that if the user is logged in, he can navigate to a view (lets say its reachable through the route /changeUserData. There, he can find a form. In this form, initally there is no data, he can enter everything (as I said, like City, Street, etc.) and save information. I'd like to save this information in another table in the db, called user_infos, with several columns containing City, Street, Telephone, etc. I'd like to be able to have this second table without a primary key, but with a foreign key - the primary key of the users table.

So the process would be, navigates to the view with the form /changeUserData. Then there is a lookup in the table user_infos, if the logged in user already saved information there. Therefore, I'd need to look up the primary key of the user via the email-adress (unique as well), which is stored in {{auth::user()->email}}. If he didn't save any information there yet, then he gets a blank form, where he can enter everything. Clicking on save, makes a new entry in the user_infos table, containing all information entered, as well as the foreign key got from the users table. If there is already an entry for the user, the information should be already shown in the form inputs, and the user should also be able to edit the information and save the updated information.

I know this was much text. Basically what I need to know is how to really do the database things. So how can I lookup the primary key (respectively the column with the name id) with the email-adress. How can I then create a new row in the other table, containing this id and the information entered? And how can I get the information from the database then to add in to the form inputs? Basically what I found is Eloquent ORM, but I doesn't seem to understand it. Can anybody give me an example of how I can do this? Or is there another way to do this?

Please note, that I'm completely new to Laravel, as well as this hole Object - Model thing, this might be the reason, why I'm confused about it.

Last updated 2 years ago.
0

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

0

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.

Last updated 8 years ago.
0

\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

0

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.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

nameless nameless Joined 26 Oct 2016

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.