删除属性时检查是否有商品使用

删除商品时删除商品分、商品相关、商品sku、商品属性、商品描述
This commit is contained in:
TL 2023-01-13 10:36:53 +08:00 committed by Edward Yang
parent 216bf84a2c
commit d0aeeb522b
5 changed files with 24 additions and 2 deletions

View File

@ -13,6 +13,7 @@ namespace Beike\Admin\Repositories;
use Beike\Models\Attribute;
use Beike\Models\AttributeValue;
use Beike\Models\ProductAttribute;
class AttributeRepo
{
@ -107,6 +108,10 @@ class AttributeRepo
public static function delete($id)
{
$productIds = ProductAttribute::query()->where('attribute_id', $id)->pluck('product_id')->toArray();
if ($productIds) {
throw New \Exception(trans('admin/attribute.error_cannot_delete_product_used', ['product_ids' => implode(', ', $productIds)]));
}
$attribute = Attribute::query()->findOrFail($id);
$attribute->descriptions()->delete();
$attribute->values()->delete();

View File

@ -14,6 +14,11 @@ namespace Beike\Repositories;
use Beike\Models\Attribute;
use Beike\Models\AttributeValue;
use Beike\Models\Product;
use Beike\Models\ProductAttribute;
use Beike\Models\ProductCategory;
use Beike\Models\ProductDescription;
use Beike\Models\ProductRelation;
use Beike\Models\ProductSku;
use Beike\Shop\Http\Resources\ProductSimple;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
@ -370,6 +375,16 @@ class ProductRepo
public static function forceDeleteTrashed()
{
Product::onlyTrashed()->forceDelete();
$products = Product::onlyTrashed();
$productsIds = $products->pluck('id')->toArray();
ProductRelation::query()->whereIn('product_id', $productsIds)->orWhere('relation_id', $productsIds)->delete();
ProductAttribute::query()->whereIn('product_id', $productsIds)->delete();
ProductCategory::query()->whereIn('product_id', $productsIds)->delete();
ProductSku::query()->whereIn('product_id', $productsIds)->delete();
ProductDescription::query()->whereIn('product_id', $productsIds)->delete();
$products->forceDelete();
}
}

View File

@ -20,4 +20,5 @@ return [
'btn_at' => 'Go now',
'btn_later' => 'Later',
'to_info_values' => 'Please go to the details page to edit attribute values',
'error_cannot_delete_product_used' => 'Attribute used by products (ID: :product_ids), can not be deleted!'
];

View File

@ -12,5 +12,5 @@
return [
'index' => 'Attribute group',
'create_at_groups' => 'Create attribute group',
'error_cannot_delete_attribute_used' => 'Attribute Group used by attribute (ID: attributes), can not be deleted!'
'error_cannot_delete_attribute_used' => 'Attribute Group used by attribute (ID: :attributes), can not be deleted!'
];

View File

@ -20,4 +20,5 @@ return [
'btn_at' => '立即前往',
'btn_later' => '稍后再去',
'to_info_values' => '请前往详情页编辑属性值',
'error_cannot_delete_product_used' => '属性不能删除由于该属性被商品商品ID: :product_ids使用'
];