| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- <?php
- namespace App;
- use Illuminate\Database\Eloquent\Collection;
- use Illuminate\Database\Eloquent\Model;
- use App\Traits\ModelTimeFormat;
- use App\Traits\ModelLogChanging;
- class Authority extends Model
- {
- use ModelLogChanging;
- use ModelTimeFormat;
- protected $fillable = ['name','remark','id_parent','alias_name','type','relevance','permission'];
- 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_names = $owners->map(function($owner){
- if (mb_strpos($owner->name,"(停用)")){
- $name = mb_substr($owner->name,0,mb_stripos($owner->name,"(停用)"));
- return "(货主:{$name})";
- }
- return "(货主:{$owner->name})";
- })->toArray();
- return $authorities->filter(function ($authority)use($owner_names){
- return !in_array($authority->alias_name,$owner_names);
- });
- }
- }
|