laravel 如何只恢复最后被软删除删除的项目
laravel how to revert only last items deleted by soft delete
我有购物车 table,其中保存了所有客户订单。
+---------------------------------------- ----------------------------------+
|编号 | user_id |产品 | created_at | updated_at | deleted_at |
+---------------------------------------- ----------------------------------+
当用户软删除时我们要软删除他卡片中的所有项目
当用户想要还原时,我们想要还原上次软删除的那些项目
我可以实现。
这是我代码中的还原函数:
public function revertBackDeletedItems(Request $request)
{
$user = apiGetUser();
if ($user == null) {
return apiAbort(401);
}
$purchaseCartCount = $user->cart()->onlyTrashed()->count();
if ($purchaseCartCount == 0) {
return [
'success' => false,
'message' => 'no items in card '
];
}
$purchaseCartDateTimeInLatestDelete = $user->cart()->onlyTrashed()->orderBy('id', 'desc')->first()['deleted_at'];
$purchaseCarts = PurchaseCart::onlyTrashed()->where('user_id', $user->id)->where('deleted_at', $purchaseCartDateTimeInLatestDelete)->get();
foreach ($purchaseCarts as $key => $purchaseCart) {
$purchaseCart->restore($purchaseCart->id);
}
return ['success' => true];
}
这是只删除最后一次删除的项目的功能吗?
public function deleteLastTrashed(Request $request)
{
$user = apiGetUser();
if ($user == null) {
return apiAbort(401);
}
$purchaseCartCount = $user->cart()->onlyTrashed()->count();
if ($purchaseCartCount == 0) {
return [
'success' => false,
'message' => 'no items in card '
];
}
$purchaseCartDateTimeInLatestDelete = $user->cart()->onlyTrashed()->orderBy('id', 'desc')->first()['deleted_at'];
$purchaseCarts = PurchaseCart::onlyTrashed()->where('user_id', $user->id)->where('deleted_at', $purchaseCartDateTimeInLatestDelete)->get();
foreach ($purchaseCarts as $key => $purchaseCart) {
$purchaseCart->forceDelete($purchaseCart->id);
}
return ['success' => true];
}
我怎样才能更好地获得 deleted_at?
这不是干净的代码?
我需要更好的方法来实现这个算法
你可以试试这个:
在购物车中添加新字段 table is_deleted 和 is_recent 因此每当用户将删除集 is_deleted = 1;并且 is_recent = 1 到您要删除的那个项目,其余 is_recent = 0;
在恢复期间获取 is_recent = 1 的项目;并保存 is_deleted = 1
你快到了:
public function deleteLastTrashed(Request $request)
{
$user = apiGetUser();
if ($user == null) {
return apiAbort(401);
}
$purchaseCartCount = $user->cart()->onlyTrashed()->count();
if ($purchaseCartCount == 0) {
return [
'success' => false,
'message' => 'no items in card '
];
}
// here you know that user has at least one soft deleted cart
$userLastCartSoftDeleted = $user->cart()->onlyTrashed()->orderBy('deleted_at', 'desc')->first();
$userLastCartSoftDeleted->forceDelete();
return ['success' => true];
}
我有购物车 table,其中保存了所有客户订单。
+---------------------------------------- ----------------------------------+
|编号 | user_id |产品 | created_at | updated_at | deleted_at |
+---------------------------------------- ----------------------------------+
当用户软删除时我们要软删除他卡片中的所有项目
当用户想要还原时,我们想要还原上次软删除的那些项目
我可以实现。
这是我代码中的还原函数:
public function revertBackDeletedItems(Request $request)
{
$user = apiGetUser();
if ($user == null) {
return apiAbort(401);
}
$purchaseCartCount = $user->cart()->onlyTrashed()->count();
if ($purchaseCartCount == 0) {
return [
'success' => false,
'message' => 'no items in card '
];
}
$purchaseCartDateTimeInLatestDelete = $user->cart()->onlyTrashed()->orderBy('id', 'desc')->first()['deleted_at'];
$purchaseCarts = PurchaseCart::onlyTrashed()->where('user_id', $user->id)->where('deleted_at', $purchaseCartDateTimeInLatestDelete)->get();
foreach ($purchaseCarts as $key => $purchaseCart) {
$purchaseCart->restore($purchaseCart->id);
}
return ['success' => true];
}
这是只删除最后一次删除的项目的功能吗?
public function deleteLastTrashed(Request $request)
{
$user = apiGetUser();
if ($user == null) {
return apiAbort(401);
}
$purchaseCartCount = $user->cart()->onlyTrashed()->count();
if ($purchaseCartCount == 0) {
return [
'success' => false,
'message' => 'no items in card '
];
}
$purchaseCartDateTimeInLatestDelete = $user->cart()->onlyTrashed()->orderBy('id', 'desc')->first()['deleted_at'];
$purchaseCarts = PurchaseCart::onlyTrashed()->where('user_id', $user->id)->where('deleted_at', $purchaseCartDateTimeInLatestDelete)->get();
foreach ($purchaseCarts as $key => $purchaseCart) {
$purchaseCart->forceDelete($purchaseCart->id);
}
return ['success' => true];
}
我怎样才能更好地获得 deleted_at?
这不是干净的代码?
我需要更好的方法来实现这个算法
你可以试试这个:
在购物车中添加新字段 table is_deleted 和 is_recent 因此每当用户将删除集 is_deleted = 1;并且 is_recent = 1 到您要删除的那个项目,其余 is_recent = 0; 在恢复期间获取 is_recent = 1 的项目;并保存 is_deleted = 1
你快到了:
public function deleteLastTrashed(Request $request)
{
$user = apiGetUser();
if ($user == null) {
return apiAbort(401);
}
$purchaseCartCount = $user->cart()->onlyTrashed()->count();
if ($purchaseCartCount == 0) {
return [
'success' => false,
'message' => 'no items in card '
];
}
// here you know that user has at least one soft deleted cart
$userLastCartSoftDeleted = $user->cart()->onlyTrashed()->orderBy('deleted_at', 'desc')->first();
$userLastCartSoftDeleted->forceDelete();
return ['success' => true];
}