1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
- namespace App\Models;
- use Encore\Admin\Traits\DefaultDatetimeFormat;
- use Encore\Admin\Traits\ModelTree;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsToMany;
- use Illuminate\Support\Facades\DB;
- class CdmsMenus extends Model
- {
- //
- use DefaultDatetimeFormat;
- use ModelTree {
- ModelTree::boot as treeBoot;
- }
- /**
- * The attributes that are mass assignable.
- *
- * @var array
- */
- protected $fillable = ['parent_id', 'order', 'title', 'icon', 'uri', 'permission'];
- /**
- * Create a new Eloquent model instance.
- *
- * @param array $attributes
- */
- public function __construct(array $attributes = [])
- {
- $connection = config('tenancy.database.connection') ?: config('database.default');
- $this->setConnection($connection);
- $this->setTable(config('tenancy.database.menu_table'));
- parent::__construct($attributes);
- }
- /**
- * A Menu belongs to many roles.
- *
- * @return BelongsToMany
- */
- public function roles(): BelongsToMany
- {
- $pivotTable = config('tenancy.database.role_menu_table');
- $relatedModel = config('tenancy.database.roles_model');
- return $this->belongsToMany($relatedModel, $pivotTable, 'menu_id', 'role_id');
- }
- /**
- * @return array
- */
- public function allNodes(): array
- {
- $connection = config('tenancy.database.connection') ?: config('database.default');
- $orderColumn = DB::connection($connection)->getQueryGrammar()->wrap($this->orderColumn);
- $byOrder = 'ROOT ASC,'.$orderColumn;
- $query = static::query();
- if (config('tenancy.check_menu_roles') !== false) {
- $query->with('roles');
- }
- return $query->selectRaw('*, '.$orderColumn.' ROOT')->orderByRaw($byOrder)->get()->toArray();
- }
- /**
- * determine if enable menu bind permission.
- *
- * @return bool
- */
- public function withPermission()
- {
- return (bool) config('tenancy.menu_bind_permission');
- }
- /**
- * Detach models from the relationship.
- *
- * @return void
- */
- protected static function boot()
- {
- static::treeBoot();
- static::deleting(function ($model) {
- $model->roles()->detach();
- });
- }
- }
|