如果条件匹配,Model::updateOrCreate() 是否会更新软删除模型?
Will Model::updateOrCreate() update a soft-deleted model if the criteria matches?
假设我有一个被软删除的模型并且有以下情况:
// EXISTING soft-deleted Model's properties
$model = [
'id' => 50,
'app_id' => 132435,
'name' => 'Joe Original',
'deleted_at' => '2015-01-01 00:00:00'
];
// Some new properties
$properties = [
'app_id' => 132435,
'name' => 'Joe Updated',
];
Model::updateOrCreate(
['app_id' => $properties['app_id']],
$properties
);
Joe Original
现在是Joe Updated
吗?
或是否有删除的记录和新的Joe Updated
记录?
updateOrCreate 将查找 deleted_at 等于 NULL 的模型,因此它不会找到软删除模型。但是,因为找不到,它会尝试创建一个新的,导致重复,这可能不是您需要的。
顺便说一句,您的代码有误。 Model::updateOrCreate 将数组作为第一个参数。
RoleUser::onlyTrashed()->updateOrCreate(
[
'role_id' => $roleId,
'user_id' => $user->id
],
[
'deleted_at' => NULL,
'updated_at' => new \DateTime()
])->restore();
像这样你创建一个新的或更新一个现有的被软删除并将软删除重置为 NULL
试试这个逻辑..
foreach ($harga as $key => $value) {
$flight = salesprice::updateOrCreate(
['customerID' => $value['customerID'],'productID' => $value['productID'], 'productCode' => $value['productCode']],
['price' => $value['price']]
);
}
对我有用
$variable = YourModel::withTrashed()->updateOrCreate(
['whereAttributes' => $attributes1, 'anotherWhereAttributes' => $attributes2],
[
'createAttributes' => $attributes1,
'createAttributes' => $attributes2,
'createAttributes' => $attributes3,
'deleted_at' => null,
]
);
创建新的或更新已软删除的现有内容并将软删除重置为 NULL
我用 Laravel 5.3 和 MySql 测试了@mathieu-dierckx 的解决方案
如果要更新的模型没有变化(即您正在尝试使用相同的旧值进行更新),updateOrCreate
方法 returns null 和 restore()
给出 Illegal offset type in isset or empty
Model::withTrashed()->updateOrCreate([
'foo' => $foo,
'bar' => $bar
], [
'baz' => $baz,
'deleted_at' => NULL
]);
按预期工作 (Laravel 5.7) - 更新现有记录并 "undeletes" 它。
我通过添加 withTrashed 让它工作,这样它将包含 soft-deleted尝试更新或创建时的项目。确保 deleted_at 在 可填充数组 [=22 中=] 你的模型。
$model = UserRole::withTrashed()->updateOrCreate([
'creator_id' => $creator->id,
'user_id' => $user->id,
'role_id' => $role->id,
],[
'deleted_at' => NULL
])->fresh();
假设我有一个被软删除的模型并且有以下情况:
// EXISTING soft-deleted Model's properties
$model = [
'id' => 50,
'app_id' => 132435,
'name' => 'Joe Original',
'deleted_at' => '2015-01-01 00:00:00'
];
// Some new properties
$properties = [
'app_id' => 132435,
'name' => 'Joe Updated',
];
Model::updateOrCreate(
['app_id' => $properties['app_id']],
$properties
);
Joe Original
现在是Joe Updated
吗?
或是否有删除的记录和新的Joe Updated
记录?
updateOrCreate 将查找 deleted_at 等于 NULL 的模型,因此它不会找到软删除模型。但是,因为找不到,它会尝试创建一个新的,导致重复,这可能不是您需要的。
顺便说一句,您的代码有误。 Model::updateOrCreate 将数组作为第一个参数。
RoleUser::onlyTrashed()->updateOrCreate(
[
'role_id' => $roleId,
'user_id' => $user->id
],
[
'deleted_at' => NULL,
'updated_at' => new \DateTime()
])->restore();
像这样你创建一个新的或更新一个现有的被软删除并将软删除重置为 NULL
试试这个逻辑..
foreach ($harga as $key => $value) {
$flight = salesprice::updateOrCreate(
['customerID' => $value['customerID'],'productID' => $value['productID'], 'productCode' => $value['productCode']],
['price' => $value['price']]
);
}
对我有用
$variable = YourModel::withTrashed()->updateOrCreate(
['whereAttributes' => $attributes1, 'anotherWhereAttributes' => $attributes2],
[
'createAttributes' => $attributes1,
'createAttributes' => $attributes2,
'createAttributes' => $attributes3,
'deleted_at' => null,
]
);
创建新的或更新已软删除的现有内容并将软删除重置为 NULL
我用 Laravel 5.3 和 MySql 测试了@mathieu-dierckx 的解决方案
如果要更新的模型没有变化(即您正在尝试使用相同的旧值进行更新),updateOrCreate
方法 returns null 和 restore()
给出 Illegal offset type in isset or empty
Model::withTrashed()->updateOrCreate([
'foo' => $foo,
'bar' => $bar
], [
'baz' => $baz,
'deleted_at' => NULL
]);
按预期工作 (Laravel 5.7) - 更新现有记录并 "undeletes" 它。
我通过添加 withTrashed 让它工作,这样它将包含 soft-deleted尝试更新或创建时的项目。确保 deleted_at 在 可填充数组 [=22 中=] 你的模型。
$model = UserRole::withTrashed()->updateOrCreate([
'creator_id' => $creator->id,
'user_id' => $user->id,
'role_id' => $role->id,
],[
'deleted_at' => NULL
])->fresh();