Authority.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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_names = $owners->map(function($owner){
  35. if (mb_strpos($owner->name,"(停用)")){
  36. $name = mb_substr($owner->name,0,mb_stripos($owner->name,"(停用)"));
  37. return "(货主:{$name})";
  38. }
  39. return "(货主:{$owner->name})";
  40. })->toArray();
  41. return $authorities->filter(function ($authority)use($owner_names){
  42. return !in_array($authority->alias_name,$owner_names);
  43. });
  44. }
  45. }