Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving one to one relation in Laravel

Tags:

A User has one (or zero) Company, and a Company belongs to one and only one User. I try to save a company for a user but it adds a new entry in database every time I re-trigger the save method. It's a one to one relation, so I though save method on User.

So Company has one method user():

public function user() {     return $this->belongsTo(User::class, 'user_id'); } 

And User has one method company():

public function company() {     return $this->hasOne(Company::class, 'user_id'); } 

I'm trying to save (so create or update) a user's company like this (in a Controller):

$company = new Company(); $company->name = 'Test'; User::findOrFail(1)->company()->save($company); 

First time I run this code it creates the entry in database (OK), but second time it adds a new entry for the same user (Not OK). I thought it will only update the database entry.

Is it a glitch (or something I don't understand in one to one relation) in Laravel or am I doing something wrong? (I think and hope it's the second purpose)

like image 559
rap-2-h Avatar asked Sep 10 '15 09:09

rap-2-h


People also ask

How do you make a one to one relationship in Laravel?

How to define One to One relationship in Laravel? Details table migration would look like following (add columns shown below): add title, description columns. define foregin key constraint in details table because it refers to post table.

How do I save a one to many relationship in Laravel?

You just need to pass the array of options with id, if id is present already in the database then it will update, if id is blank it will add a new record, If Id was there but not in your new array, then that record will get deleted.

How does Laravel save relationship with data?

You have to call the save-method manually. Alternatively you can do it with create() : Dealership::create(array('name' => Input::get('dealership_name'))); this automatically saves the newly created object so it has an ID to associate with.

What is with () in Laravel?

with() function is used to eager load in Laravel. Unless of using 2 or more separate queries to fetch data from the database , we can use it with() method after the first command. It provides a better user experience as we do not have to wait for a longer period of time in fetching data from the database.


2 Answers

Creating and updating need to treat differently. So check the existence of company attribute first.

$user = User::with('company')->findOrFail(1); if ($user->company === null) {     $company = new Company(['name' => 'Test']);     $user->company()->save($company); } else {     $user->company->update(['name' => 'Test']); } 

Note that hasOne() does not guarantee that you will have one-to-one relationship, it just telling Eloquent how to create query. It works even you have multiple Company refer to same User, in such case when you call $user->company you will get first Company in the result data set from database.

like image 144
PaePae Avatar answered Jan 04 '23 06:01

PaePae


$user = User::findOrFail(1); $company = $user->company ?: new Company; $company->name = 'Test'; $user->company()->save($company); 
like image 26
MstfAsan Avatar answered Jan 04 '23 06:01

MstfAsan