✅ CourseCategoryController (resource) ✅ CourseCategoryPolicy ✅ Маршруты: /admin/course-categories ✅ Blade-шаблоны: - index.blade.php (древовидный список) - create.blade.php - _row.blade.php (partial для дерева) ✅ Ссылка в сайдбаре ✅ Вложенность категорий (parent/children) ✅ Scope tree() для древовидной структуры Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
49 lines
1.1 KiB
PHP
Executable File
49 lines
1.1 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
class CourseCategory extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'parent_id',
|
|
'name',
|
|
'slug',
|
|
'description',
|
|
'sort_order',
|
|
'is_active',
|
|
];
|
|
|
|
protected $casts = [
|
|
'is_active' => 'boolean',
|
|
];
|
|
|
|
public function parent(): BelongsTo
|
|
{
|
|
return $this->belongsTo(CourseCategory::class, 'parent_id');
|
|
}
|
|
|
|
public function children(): HasMany
|
|
{
|
|
return $this->hasMany(CourseCategory::class, 'parent_id');
|
|
}
|
|
|
|
public function courses(): HasMany
|
|
{
|
|
return $this->hasMany(Course::class);
|
|
}
|
|
|
|
public function scopeTree($query)
|
|
{
|
|
return $query->with(['children', 'children.children'])->withCount('courses')->orderBy('sort_order')->get()->filter(function($cat) {
|
|
return is_null($cat->parent_id);
|
|
});
|
|
}
|
|
}
|