删除分类

This commit is contained in:
Edward Yang 2022-08-12 15:12:22 +08:00
parent 0ce474df27
commit a818c0c9f7
3 changed files with 38 additions and 2 deletions

View File

@ -2,7 +2,6 @@
namespace Beike\Admin\Http\Controllers;
use Beike\Models\Category;
use Illuminate\Http\Request;
use Beike\Repositories\CategoryRepo;
@ -10,7 +9,6 @@ use Beike\Admin\Services\CategoryService;
use Beike\Admin\Http\Requests\CategoryRequest;
use Beike\Admin\Http\Resources\CategoryResource;
class CategoryController extends Controller
{
protected string $defaultRoute = 'categories.index';
@ -48,6 +46,21 @@ class CategoryController extends Controller
return $this->save($request, $category);
}
/**
* 删除分类
* @param Request $request
* @param Category $category
* @return array
* @throws \Exception
*/
public function destroy(Request $request, Category $category): array
{
CategoryRepo::delete($category);
return json_success('删除成功');
}
public function name(int $id)
{
$name = CategoryRepo::getName($id);

View File

@ -39,4 +39,9 @@ class Category extends Base
{
return $this->hasMany(CategoryPath::class);
}
public function productCategories(): HasMany
{
return $this->hasMany(ProductCategory::class);
}
}

View File

@ -98,6 +98,24 @@ class CategoryRepo
}
/**
* 删除产品分类
* @throws \Exception
*/
public static function delete($category)
{
if (is_int($category)) {
$category = Category::query()->findOrFail($category);
} elseif (!($category instanceof Category)) {
throw new \Exception('invalid category');
}
$category->descriptions()->delete();
$category->paths()->delete();
$category->productCategories()->delete();
$category->delete();
}
public static function getName($id)
{
$category = Category::query()->find($id);