我使用了laravel 5.6,并使用updateOrCreate
模型添加或更新了一些数据。
但我需要获取所有更改过的值
$q=Userssub::updateOrCreate(
['userid' => $uid ],
['model' => $model]
);
结果如下图所示
如何获取更改数组?
我试着用
$u->changes
和
$u->changes->toarray()
但两者都返回null。
如何才能获取更改后的值?
发布于 2018-06-26 01:38:56
能言善辩的模型有两个受保护的数组,$original
和$changes
,它们分别包含从存储中获取的属性和已修改的属性。
因此,您可以使用getOriginal()
和getChanges()
来比较它们之间的差异。
$model = Model::createOrUpdate([...]);
// wasRecentlyCreated is a boolean indicating if the model was inserted during the current request lifecycle.
if (!$model->wasRecentlyCreated) {
$changes = $model->getChanges();
}
发布于 2018-11-22 10:33:29
这将创建一个数组,该数组将包含原始属性值及其更改后的值:
if (!$model->wasRecentlyCreated) {
$original = $model->getOriginal();
$changes = [];
foreach ($model->getChanges() as $key => $value) {
$changes[$key] = [
'original' => $original[$key],
'changes' => $value,
];
}
}
例如:
(
[first_name] => [
[original] => Kevinn
[changes] => Kevin
]
[website] => [
[original] => google.com
[changes] => google.ca
]
)
https://stackoverflow.com/questions/51028944
复制相似问题