| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- <?php
- namespace App;
- use Illuminate\Database\Eloquent\Collection;
- use Illuminate\Database\Eloquent\Model;
- use App\Traits\ModelTimeFormat;
- use App\Traits\ModelLogChanging;
- use Illuminate\Database\Eloquent\SoftDeletes;
- use Illuminate\Support\Facades\DB;
- class Authority extends Model
- {
- use ModelLogChanging;
- use SoftDeletes;
- use ModelTimeFormat;
- protected $fillable = ['name','parent_id','alias_name','permission',"route","method"];
- const METHOD = [
- 0 => "GET",
- 1 => "POST",
- 2 => "PUT",
- 3 => "DELETE",
- 4 => "ANDROID"
- ];
- function roles(){
- return $this->belongsToMany('App\Role','authority_role','id_authority','id_role');
- }
- function getNameFilteredAttribute(){
- preg_match('#(.*)(_[0-9]*?$)#',$this['name'],$arr);
- if($arr){
- $id=str_replace('_','',$arr[2]);
- $owner = Owner::find($id);
- if($owner){return "{$arr[1]}({$owner['name']})";}
- }
- return $this['name'];
- }
- function getOwnerIdAttribute(){
- preg_match('#_([0-9]*?)$#',$this['name'],$arr);
- if(count($arr)>1&&$arr[1]){
- return $arr[1];
- }
- return '';
- }
- public static function filterRecycle(Collection $authorities)
- {
- $owners = Owner::query()->whereNotNull("deleted_at")->get();
- $owner_keys = $owners->map(function($owner){
- return '_'.$owner['id'];
- })->toArray();
- return $authorities->filter(function ($authority)use($owner_keys){
- return !in_array($authority->name,$owner_keys);
- });
- }
- }
|