123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\SoftDeletes;
- use Encore\Admin\Traits\ModelTree;
- use Encore\Admin\Traits\AdminBuilder;
- use Illuminate\Database\Eloquent\Relations\BelongsToMany;
- use Encore\Admin\Facades\Admin;
- use Illuminate\Foundation\Auth\User;
- use App\Models\ArticleCategoryRole;
- use App\Models\Role;
- class ArticleCategory extends Model
- {
- use SoftDeletes,AdminBuilder,ModelTree {
- ModelTree::boot as treeBoot;
- }
- protected $table = 'article_categorys';
- public function __construct(array $attributes = [])
- {
- parent::__construct($attributes);
- $this->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'];
- }
- }
-
- $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;
- }
- }
|