*/ protected $fillable = [ 'name', 'username', 'email', 'password', 'company_id', 'department_id', 'role_id', 'created_by', 'gender', 'address', 'phone', 'status', 'fs_password', 'leader_id', ]; //因为CompanyScope要拿到当前用户,但是user模型还没创建完无法使用CompanyScope protected static function booted(): void { //创建用户 static::creating(function (User $user){ $result=User::query()->where('company_id',$user->company_id)->max('display_id'); $displayIndex=$result >0? $result+1 :1; $user->display_id =$displayIndex; }); //更改用户所属公司 static::updating(function (User $user){ if($user->isDirty('company_id')){ $result=User::query()->where('company_id',$user->company_id)->max('display_id'); $displayIndex=$result >0? $result+1 :1; $user->display_id =$displayIndex; } }); } /** * The attributes that should be hidden for serialization. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'email_verified_at' => 'datetime', 'password' => 'hashed', ]; protected function superAdmin(): Attribute { return Attribute::make( get: fn() => $this->role_id == config("auth.super_admin_role_id"), ); } public function scopeAllowed(Builder $query) { if (!Auth::user()->super_admin) { $query->where("company_id", Auth::user()->company_id); } } public function guardName(): string { return $this->guard_name; } public function company(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(Company::class); } public function role(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(Role::class); } public function createdBy(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(User::class, 'created_by'); } public function department(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(Department::class); } }