Files
ahrommag/app/Models/Category.php
2025-11-16 12:43:07 +03:30

232 lines
6.1 KiB
PHP

<?php
namespace App\Models;
use App\Models\CategoryObject;
use Illuminate\Database\Eloquent\Model;
use App\Models\Category as Modelcategory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Category extends Model
{
use SoftDeletes;
protected $fillable = [
'title',
'slug',
'image_url',
'status',
'is_news',
'parent_id',
'parent_below',
'color',
'priority',
];
protected $casts = [
'is_news' => 'boolean',
'status' => 'boolean'
];
public function getAllDescendantIds(): array
{
$ids = collect([$this->id]);
$children = Category::where('parent_id', $this->id)->get();
foreach ($children as $child) {
$ids = $ids->merge($child->getAllDescendantIds());
}
$subcategories = Category::where('parent_below', $this->id)->get();
foreach ($subcategories as $subcategory) {
$ids = $ids->merge($subcategory->getAllDescendantIds());
}
return $ids->unique()->all();
}
public static function getHierarchicalCategoriesForFilter()
{
$allCategories = [];
$rootCategories = self::whereNull('parent_id')
->whereNull('parent_below')
->with(['children', 'subcategories'])
->orderBy('is_news', 'desc')
->orderBy('title')->get();
foreach ($rootCategories as $item) {
self::addCategoryToHierarchyList($item, 0, $allCategories);
}
return $allCategories;
}
private static function addCategoryToHierarchyList(Category $category, int $level, array &$list)
{
$category->level = $level;
$list[] = $category;
$descendants = $category->children->merge($category->subcategories)->sortBy('title');
foreach ($descendants as $descendant) {
self::addCategoryToHierarchyList($descendant, $level + 1, $list);
}
}
public function blogs()
{
return $this->hasMany(Blog::class);
}
public function mag_news($categoryId = null)
{
$id = $categoryId ?? $this->id;
return Blog::query()
->where('status', 1)
->where('notBlog', 1)
->where('category_array', 'like', '%"' . $id . '"%')
->orderByDesc('published_at')
->get()->take(4);
}
public function mag_blogs($limit = 4)
{
return Blog::query()
->where('status', 1)
->whereNull('notBlog')
->where(function($query) {
$query->where('category_id', $this->id)
->orWhereJsonContains('category_array', (string)$this->id);
})
->orderByDesc('published_at')
->take($limit)
->get();
}
public function hasBlogs()
{
return Blog::where('status', 1)
->whereNull('notBlog')
->where(function($query) {
$query->where('category_id', $this->id)
->orWhereJsonContains('category_array', (string)$this->id);
})
->exists();
}
public function blog_category()
{
$blogs = Blog::where('notBlog', 1)->orderBy('created_at', 'desc') ->get();
return $blogs->filter(function($blog) {
$categoryArray = $blog->category_array ;
return isset($categoryArray[0]) && $categoryArray[0] == $this->id;
});
}
public function child()
{
return $this->hasOne(Modelcategory::class , 'parent_id' , 'id');
}
public function isRoot(): bool
{
return is_null($this->parent_id) && is_null($this->parent_below);
}
public static function getNewsRootCategories()
{
return self::where('is_news', true)
->whereNull('parent_id')
->whereNull('parent_below')
->get();
}
public static function getArticleRootCategories()
{
return self::where('is_news', false)
->whereNull('parent_id')
->whereNull('parent_below')
->get();
}
public function getFullHierarchy()
{
$hierarchy = collect([$this]);
if ($this->parent()->exists()) {
$hierarchy = $this->parent()->first()->getFullHierarchy()->merge($hierarchy);
}
if ($this->mainCategory()->exists()) {
$hierarchy = $this->mainCategory()->first()->getFullHierarchy()->merge($hierarchy);
}
return $hierarchy->unique('id');
}
public function children(): HasMany
{
return $this->hasMany(Category::class, 'parent_id')->orderByDesc('priority');
}
// Recursive relation to load all descendants infinitely
public function childrenRecursive()
{
return $this->children()->with('childrenRecursive');
}
public function subcategories(): HasMany
{
return $this->hasMany(Category::class, 'parent_below')->orderByDesc('priority');
}
public function parent(): BelongsTo
{
return $this->belongsTo(Category::class, 'parent_id');
}
public function fparent(): BelongsTo
{
return $this->belongsTo(Category::class, 'parent_id');
}
public function mainCategory(): BelongsTo
{
return $this->belongsTo(Category::class, 'parent_below');
}
public function getBreadcrumbAttribute(): string
{
$breadcrumbs = [];
if ($this->parent_id) {
$parent = $this->parent ?? Category::find($this->parent_id);
if ($parent) {
$breadcrumbs[] = $parent->breadcrumb;
}
}
if ($this->parent_below) {
$mainCategory = $this->mainCategory ?? Category::find($this->parent_below);
if ($mainCategory) {
$breadcrumbs[] = $mainCategory->breadcrumb;
}
}
$breadcrumbs[] = $this->title;
return implode(' > ', array_unique($breadcrumbs));
}
public function categoryObjects(): HasMany
{
return $this->hasMany(CategoryObject::class);
}
}