Authority.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Collection;
  4. use Illuminate\Database\Eloquent\Model;
  5. use App\Traits\ModelTimeFormat;
  6. use App\Traits\ModelLogChanging;
  7. class Authority extends Model
  8. {
  9. use ModelLogChanging;
  10. use ModelTimeFormat;
  11. protected $fillable = ['name','remark','id_parent','alias_name','type','relevance','permission'];
  12. function roles(){
  13. return $this->belongsToMany('App\Role','authority_role','id_authority','id_role');
  14. }
  15. function getNameFilteredAttribute(){
  16. preg_match('#(.*)(_[0-9]*?$)#',$this['name'],$arr);
  17. if($arr){
  18. $id=str_replace('_','',$arr[2]);
  19. $owner = Owner::find($id);
  20. if($owner){return "{$arr[1]}({$owner['name']})";}
  21. }
  22. return $this['name'];
  23. }
  24. function getOwnerIdAttribute(){
  25. preg_match('#_([0-9]*?)$#',$this['name'],$arr);
  26. if(count($arr)>1&&$arr[1]){
  27. return $arr[1];
  28. }
  29. return '';
  30. }
  31. public static function filterRecycle(Collection $authorities)
  32. {
  33. $owners = Owner::query()->whereNotNull("deleted_at")->get();
  34. $owner_keys = $owners->map(function($owner){
  35. return '_'.$owner['id'];
  36. })->toArray();
  37. return $authorities->filter(function ($authority)use($owner_keys){
  38. return !in_array($authority->name,$owner_keys);
  39. });
  40. }
  41. }