我有一个如下的数组
'topic' =>
array (
'id' => 13,
'title' => 'Macros',
'content' => '<p>Macros. This is the updated content.</p>
',
'created_at' => '2014-02-28 18:36:55',
'updated_at' => '2014-05-14 16:42:14',
'category_id' => '5',
'tags' => 'tags',
'referUrl' => '',
'user_id' => 3,
'videoUrl' => '',
'useDefaultVideoOverlay' => 'true',
'positive' => 0,
'negative' => 1,
'context' => 'macros',
'viewcount' => 60,
'deleted_at' => NULL,
)
我想使用这个数组,并将其转换/强制转换为专题模型。有没有办法可以做到这一点。谢谢
$model = \App\Models\TblTask::find(1);
$model->fill($updatedTaskFields);
$model->save();
同时,这也适用于从数据库更新记录的情况。 - Christian Noel->fill(array $attributes)
用于更新现有模型?顺便说一下,你可能想使用->findOrFail(int $id)
,如果未找到ID,它将抛出异常。不过这取决于你的用例。 - Henk Poley->update(array $attributes = [], array $options = [])
,它执行的是$this->fill($attributes)->save($options);
。 - Henk Poley