清理后台分类数据
This commit is contained in:
parent
aa76941cf7
commit
97f3db810a
|
|
@ -15,10 +15,7 @@ class CategoryController extends Controller
|
||||||
|
|
||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
$categories = Category::with('description', 'children.description', 'children.children.description')
|
$categories = CategoryRepo::getAdminList();
|
||||||
->where('parent_id', 0)
|
|
||||||
->get();
|
|
||||||
|
|
||||||
$data = [
|
$data = [
|
||||||
'categories' => CategoryResource::collection($categories),
|
'categories' => CategoryResource::collection($categories),
|
||||||
];
|
];
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace Beike\Models;
|
namespace Beike\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
|
@ -20,6 +21,11 @@ class Category extends Base
|
||||||
'active' => 'boolean',
|
'active' => 'boolean',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
public function parent(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Category::class, 'parent_id');
|
||||||
|
}
|
||||||
|
|
||||||
public function children(): HasMany
|
public function children(): HasMany
|
||||||
{
|
{
|
||||||
return $this->hasMany(Category::class, 'parent_id');
|
return $this->hasMany(Category::class, 'parent_id');
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,41 @@ class CategoryRepo
|
||||||
private static $allCategoryWithName = null;
|
private static $allCategoryWithName = null;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 后台获取分类列表
|
||||||
|
* @return Builder[]|Collection
|
||||||
|
*/
|
||||||
|
public static function getAdminList()
|
||||||
|
{
|
||||||
|
self::cleanCategories();
|
||||||
|
return Category::with(['description', 'children.description', 'children.children.description'])
|
||||||
|
->where('parent_id', 0)
|
||||||
|
->get();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清理分类数据
|
||||||
|
*/
|
||||||
|
public static function cleanCategories()
|
||||||
|
{
|
||||||
|
$categories = Category::with([
|
||||||
|
'parent',
|
||||||
|
'description',
|
||||||
|
])->get();
|
||||||
|
|
||||||
|
foreach ($categories as $category) {
|
||||||
|
if ($category->parent_id && empty($category->parent)) {
|
||||||
|
$category->parent_id = 0;
|
||||||
|
$category->save();
|
||||||
|
}
|
||||||
|
if (empty($category->description)) {
|
||||||
|
$category->delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public static function flatten(string $locale, $separator = ' > '): array
|
public static function flatten(string $locale, $separator = ' > '): array
|
||||||
{
|
{
|
||||||
$sql = "SELECT cp.category_id AS id, TRIM(LOWER(GROUP_CONCAT(cd1.name ORDER BY cp.level SEPARATOR '{$separator}'))) AS name, c1.parent_id, c1.position";
|
$sql = "SELECT cp.category_id AS id, TRIM(LOWER(GROUP_CONCAT(cd1.name ORDER BY cp.level SEPARATOR '{$separator}'))) AS name, c1.parent_id, c1.position";
|
||||||
|
|
@ -108,6 +143,10 @@ class CategoryRepo
|
||||||
if ($pathName) {
|
if ($pathName) {
|
||||||
$pathName .= ' > ';
|
$pathName .= ' > ';
|
||||||
}
|
}
|
||||||
|
if (empty($path->pathCategory)) {
|
||||||
|
$path->delete();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
$pathName .= $path->pathCategory->description->name;
|
$pathName .= $path->pathCategory->description->name;
|
||||||
}
|
}
|
||||||
$results[] = [
|
$results[] = [
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue