diff --git a/beike/Admin/Repositories/AttributeRepo.php b/beike/Admin/Repositories/AttributeRepo.php index 1c361aa9..0089eddb 100644 --- a/beike/Admin/Repositories/AttributeRepo.php +++ b/beike/Admin/Repositories/AttributeRepo.php @@ -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(); diff --git a/beike/Repositories/ProductRepo.php b/beike/Repositories/ProductRepo.php index d3ad7d40..6323dfd2 100644 --- a/beike/Repositories/ProductRepo.php +++ b/beike/Repositories/ProductRepo.php @@ -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(); } } diff --git a/resources/lang/en/admin/attribute.php b/resources/lang/en/admin/attribute.php index f1d380ef..c09b5b2d 100644 --- a/resources/lang/en/admin/attribute.php +++ b/resources/lang/en/admin/attribute.php @@ -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!' ]; diff --git a/resources/lang/en/admin/attribute_groups.php b/resources/lang/en/admin/attribute_groups.php index d3a87b87..3b7fefaf 100644 --- a/resources/lang/en/admin/attribute_groups.php +++ b/resources/lang/en/admin/attribute_groups.php @@ -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!' ]; diff --git a/resources/lang/zh_cn/admin/attribute.php b/resources/lang/zh_cn/admin/attribute.php index 68336795..edebfce2 100644 --- a/resources/lang/zh_cn/admin/attribute.php +++ b/resources/lang/zh_cn/admin/attribute.php @@ -20,4 +20,5 @@ return [ 'btn_at' => '立即前往', 'btn_later' => '稍后再去', 'to_info_values' => '请前往详情页编辑属性值', + 'error_cannot_delete_product_used' => '属性不能删除,由于该属性被商品(商品ID: :product_ids)使用' ];