laravel4 更新数据透视表中的附加列 [英] laravel4 update additional columns in pivot table

查看:23
本文介绍了laravel4 更新数据透视表中的附加列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以多对多关系更新数据透视表中的其他列数据.

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.

我有一个对象:'$reservation' 从那个对象我创建了另一个对象 $resources 使用:

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

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

然后我使用 foreach 循环遍历 $resources,如下

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

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

然后我想更新一个名为 gcal_id 的列并使用以下内容:

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

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

如果我 var_dump 模型,我可以看到属性存在于正确的值,但在数据库本身中,条目没有被更新 - 所以保存不起作用

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?

谢谢

推荐答案

之后你在pivot上设置属性:

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();

如果要保存pivot,只保存资源是不够的.改为在 pivot 上调用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆