Authority.php 1.4 KB

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