laravel4更新数据透视表中的附加列 [英] laravel4 update additional columns in pivot table
问题描述
我有两个表 - 预留和资源链接到一个枢纽表。我可以附上和正在使用的模型。但是我正在努力更新数据透视表中的其他列之一。
我有一个对象:'$ reservation'从该对象我创建另一个对象$ resources使用:
$ resources = $ reservation-> resource() - > get();
然后我再循环 $ resources
使用 foreach
循环如下
foreach($ resources as $ resource ){...}
然后,我想更新一个名为gcal_id的列,并使用以下命令:
$ resource-> pivot-> gcal_id =TEST;
$ resource-> save();
如果我的var_dump模型我可以看到属性存在于正确的值,但在数据库本身条目没有被更新 - 所以保存不起作用
我在这两个关系中列出了列:
- &p。withPivot('start','end','quantity','product_id','gcal_id')
鉴于我有资源对象,如何在数据透视表中正确更新列,并保存到数据库?
谢谢
之后,您可以在枢轴上设置属性:
$ resource-> pivot-> gcal_id =TEST;
您似乎保存了资源,而不是支点:
$ resource-> save();
如果要保存枢轴,保存资源是不够的。
$ resource-> pivot-> gcal_id =测试;
$ resource-> pivot-> save();
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
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();
这篇关于laravel4更新数据透视表中的附加列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!