Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

laravel - laravel4 update additional columns in pivot table

I'm trying to update additional column data in a pivot table in a many to many relationship.

I have two tables - reservation and resource linked with a pivot table. I can attach and am working with the model. However I'm struggling to update one of the additional columns in the pivot table.

I have an object: '$reservation' From that object I created another object $resources using:

$resources = $reservation->resource()->get();

I'm then iterating through $resources using a foreach loop as follows

foreach($resources as $resource ) {...}

I then want to update a column called gcal_id and am using the following:

$resource->pivot->gcal_id = "TEST";
$resource->save();

If I var_dump the model I can see the property exists to the correct value but in the database itself the entry is not being updated - so the save is not working

I have the columns listed in both sides of the relationship with this:

->withPivot('start', 'end', 'quantity', 'product_id','gcal_id')

Given I have the resource object how can I update a column correctly in the pivot table and save to database?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

After that you set the attribute on the pivot:

$resource->pivot->gcal_id = "TEST";

You seem to save the resource and not the pivot:

$resource->save();

If you want the pivot to be saved, saving the resource is not enough. Call save on the pivot instead:

$resource->pivot->gcal_id = "TEST";
$resource->pivot->save();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...