64 lines
1.5 KiB
PHP
64 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use RuLong\Panel\Extensions\Tree;
|
|
use RuLong\Panel\Models\Storage;
|
|
|
|
class Category extends Model
|
|
{
|
|
|
|
use SoftDeletes;
|
|
|
|
public function parent()
|
|
{
|
|
return $this->belongsTo(Category::class);
|
|
}
|
|
|
|
public function children()
|
|
{
|
|
return $this->hasMany(Category::class, 'parent_id');
|
|
}
|
|
|
|
public function storage()
|
|
{
|
|
return $this->belongsTo(Storage::class)->withDefault();
|
|
}
|
|
|
|
public function goods()
|
|
{
|
|
return $this->hasMany(Goods::class);
|
|
}
|
|
|
|
public static function treeSelect($parent_id = 0)
|
|
{
|
|
$menus = self::orderBy('sort', 'asc')->get()->toArray();
|
|
$menus = Tree::toFormatTree($menus, 'title', 'id', 'parent_id', $parent_id);
|
|
return $menus;
|
|
}
|
|
|
|
public static function treeShow($id = 0)
|
|
{
|
|
$menus = self::when($id, function ($query) use ($id) {
|
|
return $query->where('id', '<>', $id);
|
|
})->orderBy('sort', 'asc')->get()->toArray();
|
|
|
|
$menus = Tree::toFormatTree($menus);
|
|
|
|
$menus = array_merge([0 => ['id' => 0, 'title_show' => '顶级分类']], $menus);
|
|
return $menus;
|
|
}
|
|
|
|
//查找顶级分类id
|
|
public static function findTop($id, $topPatentid)
|
|
{
|
|
$category_ids = Category::orderBy('id', 'desc')->pluck('parent_id', 'id');
|
|
do {
|
|
$id = $category_ids[$id];
|
|
} while ($category_ids[$id] != $topPatentid);
|
|
|
|
return $id;
|
|
}
|
|
}
|