欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Saving with relations (hasOne, hasMany)

I have this schema Image

User and Business One-To-One relation
Dealership and Business is One-To-Many relation

User Model

public function business() {
    return $this->hasOne(Business::class);
}

Business Model

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

public function dealership() {
    return $this->belongsTo(Dealership::class);
}

Dealership Model

public function businsses() {
    return $this->hasMany(Business::class);
}

I am trying to: create user, create business. Link the businss to the user. Create dealership and link it to the business.

Working:

$user = new User();
$user->name = 'John Doe';
// rest of the user details
$user->save();

$business = new Business();
$businss->name = 'JohnDoe Inc.';
// rest of businss details

$user->business()->save($business);
// this is working fine


$dealership = new Dealership();
$dealership->name = 'Ford/Lincoln Geneva';
// rest of dealership details

$business->dealership()->save($dealership);
// not working

How can I make the link between dealership and business?

(As Selected By cristian9509)
JarekTkaczyk

@cristian9509 The other way around. You need dealership saved first, then you can assign business.

In other words:

// save user and business
// do what you need with the dealership, then..
$dealership->save();

$dealership->businesses()->save($business);

You may want to do this, to save one query:

// user stuff
$user->save();

// dealer stuff
$dealership->save();

// business stuff
$business->user()->associate($user);
$business->dealership()->associate($dealership);
$business->save();
JarekTkaczyk

@cristian9509 The other way around. You need dealership saved first, then you can assign business.

In other words:

// save user and business
// do what you need with the dealership, then..
$dealership->save();

$dealership->businesses()->save($business);

You may want to do this, to save one query:

// user stuff
$user->save();

// dealer stuff
$dealership->save();

// business stuff
$business->user()->associate($user);
$business->dealership()->associate($dealership);
$business->save();
Goldenstat

This is just a quick look but I think you'll have to do $dealership->save();

Then $buissness->dealership()->attach($dealership->Id);

cristian9509

@JarekTkaczyk First option is not working because I am using user_id on the business table rather than an id. However the second option worked nice. Why would first option not work?

bugsysha

Cause you can not call the save method on related model/object. You need to call attach as @Goldenstatmentioned.

JarekTkaczyk

@cristian9509 Both work. If you have problem, then show the code and error - don't work says nothing ;)

@bugsysha 1 you can call save on any model, 2 it is calling save on HasMany relation, 3 attach will throw exception - it's on BelongsToMany relation, not on HasMany.

cristian9509

@JarekTkaczyk Here is the code:

$user = $request->user();

        $business = new Business();
        $business->name = 'JohnDoe Inc.'
        $business->save();


        $dealership = new Dealership();
        $dealership->name = 'Ford/Lincoln Dealer';
        $dealership->save()

        $dealership->businesses()->save($business);

And the error:

QueryException in Connection.php line 636: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: update `businesses` set `dealership_id` = 1, `updated_at` = 2015-09-16 22:15:55 where `id` = 0)

First of all the id in the where clause should be user_id. Secondly, the business does not have an id column because as I said I use the user_id. Thirdly, businesses user_id is not autoincrementing. So the businesses table must have a user connected to it to exist.

JarekTkaczyk

@cristian9509 You're saving $broker- what's that?

And You just answered your own question - you need to set PK of the model to user_id, otherwise it will try to update using default id column.

cristian9509

@JarekTkaczyk my Business model does not have a PK. I tried setting it on the migration and was getting an error. I'll try again maybe I was doing something wrong. The $broker was a bad copy paste with another variable. I corrected the code.

JarekTkaczyk

@cristian9509 It doesn't matter whether it's auto incrementing or not here. You're getting error because you didn't set protected $primaryKey = 'user_id' in that model, so eloquent tries to update using id.

cristian9509

@JarekTkaczyk Now it makes sense. But can I set that primary key in the model and still not have it set in the migration?

bobbybouwmann

You still need to create the incremental key in your migration and add it to your model

bugsysha

@JarekTkaczyk Last time I used it it did not work. Will try it again when it comes up. Hope you are right cause that would be so much easier.

cristian9509

@JarekTkaczyk Both options work fine! I added the protected $primary = 'user_id'; , changed my migration to have 'user_id' as primary key and now I can do both options. However I feel that the second option is much clearer. It's easier for when I read my code later on. Thank you.

Please sign in or create an account to participate in this conversation.

来自 https://laracasts.com/discuss/channels/eloquent/saving-with-relations-hasone-hasmany?page=1

普通分类: