【发布时间】:2014-08-03 16:54:22
【问题描述】:
使用 eloquent 模型,您只需调用即可更新数据
$model->update( $data );
但不幸的是,这不会更新关系。
如果您也想更新关系,则需要手动分配每个值并调用 push() 然后:
$model->name = $data['name'];
$model->relationship->description = $data['relationship']['description'];
$model->push();
尽管如此,如果您有大量数据要分配,它将变得一团糟。
我正在寻找类似的东西
$model->push( $data ); // this should assign the data to the model like update() does but also for the relations of $model
有人可以帮帮我吗?
【问题讨论】:
-
没有办法解决这个问题,但是你有没有尝试过这样的事情:
$model->relationship->fill($data['relationship']);然后push? -
这就是我目前所做的,但我想知道是否有更优雅的方式来做到这一点:)
-
没有其他方法,因为 Eloquent 目前不知道模型上的关系,直到您将它们称为动态属性、使用
load方法加载、急切加载等(仅推送有效)与模型的relations数组中存在的加载关系)
标签: php laravel model eloquent