setParentColumn('parent_id'); $this->setOrderColumn('list_order'); $this->setTitleColumn('category_name'); } public function parent() { return $this->belongsTo(ArticleCategory::class, 'parent_id', 'id'); } public function children() { return $this->hasMany(ArticleCategory::class, 'parent_id', 'id'); } public function roles() : BelongsToMany { $pivotTable = 'article_category_roles'; $relatedModel = config('admin.database.roles_model'); return $this->belongsToMany($relatedModel, $pivotTable, 'article_category_id', 'role_id'); } protected static function boot() { static::treeBoot(); static::deleting(function ($model) { $model->roles()->detach(); }); } protected static function categoryIds() { $is_admin = Admin::user()->isAdministrator(); if ($is_admin) { $not_ids = null; } else { $user_roles = Admin::user()->roles->toArray(); $roles_ids = array(); if ($user_roles) { foreach ($user_roles as $k => $v) { $roles_ids[] = $v['pivot']['role_id']; } } //获取对应的分类id $rst = ArticleCategoryRole::whereIn('role_id', $roles_ids)->get(array('article_category_id'))->toArray(); $role_cates = array(); if ($rst) { foreach ($rst as $k => $v) { $role_cates[] = $v['article_category_id']; } } $not_cates = ArticleCategoryRole::whereNotIn('article_category_id', $role_cates)->get(array('article_category_id'))->toArray(); $not_ids = array(); if ($not_cates) { foreach ($not_cates as $k => $v) { $not_ids[] = $v['article_category_id']; } } } return $not_ids; } }