~~~
<?php
namespace App\Models\Auth;
use App\Traits\SerializeListTime;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use App\Services\AdminUsersService;
use Tymon\JWTAuth\Contracts\JWTSubject;
/**
* AdminUsersModel 管理员表
*/
class AdminUsers extends Authenticatable implements JWTSubject
{
use Notifiable, SerializeListTime;
/**
* 开启软删除
*/
use SoftDeletes;
/**
* 数据表名称
* @var string
*/
protected $table = "admin_users";
/**
* 验证的字段
*
* @var string[]
*/
protected $fillable = [
'name',
'sex',
'person_sign',
'username',
'email',
'type',
'status',
'password',
'password_modify'
];
/**
* 查询到的数据中需要被隐藏的字段
*
* @var string[]
*/
protected $hidden = ['password'];
/**
* Get the identifier that will be stored in the subject claim of the JWT.
*
* @return mixed
*/
public function getJWTIdentifier()
{
return $this->getKey();
}
/**
* Return a key value array, containing any custom claims to be added to the JWT.
*
* @return array
*/
public function getJWTCustomClaims()
{
return [];
}
/**
* @return AdminUsers
*/
public static function getInstance(): AdminUsers
{
static $models = [];
$class = get_called_class();
if (!isset($models[$class])) {
$models[$class] = new $class();
}
return $models[$class];
}
/**
* 触发器(添加) 使用触发器在插入用户的时候为密码加密
*/
protected static function boot()
{
parent::boot();
// 在创建用户前,为用户的密码进行加密
static::creating(function ($model) {
$model->password = AdminUsersService::getPassword($model->password);
});
// 在更新用户前,为用户的密码进行加密
static::updating(function ($model) {
// 判断密码是否改变
if ($model->isDirty('password')) {
$model->password = AdminUsersService::getPassword($model->password);
}
});
}
/**
* 用户所属的角色组
*
* @return HasManyThrough
*/
public function roleGroups(): HasManyThrough
{
return $this->hasManyThrough(
AdminRoleGroup::class,
AdminUserHasRoleGroup::class,
'admin_id',
'id',
'id',
'group_id'
);
}
/**
* 用户拥有的角色
*
* @return HasManyThrough
*/
public function roles(): HasManyThrough
{
return $this->hasManyThrough(
AdminRole::class,
AdminUserHasRole::class,
'admin_id',
'id',
'id',
'role_id'
);
}
/**
* 关联角色表(多对多)
*
* @return BelongsToMany
*/
public function relationRoles(): BelongsToMany
{
return $this->belongsToMany(AdminRole::class, (new AdminRoleMenu())->getTable());
}
/**
* 搜索查询
*
* @param Builder $builder
* @param array $selectFields 查询字段数组
* @param array $orderBy 排序字段数组
* @param int $pageSize 每页数量
* @return array
*/
public function scopeSearch(
Builder $builder,
array $selectFields,
array $orderBy,
int $pageSize
): array
{
// 1:字段查询
$builder->select($selectFields)->with(['relationRoles' => function ($query) {
return $query->select(['id', 'name']);
}]);
// 2:排序查询
if (!empty($orderBy)) {
$orderBy['order'] = $orderBy['order'] == 'ascending' ? 'asc' : 'desc';
} else {
$orderBy = ["prop" => "id", "order" => "desc"];
}
$builder->orderBy($orderBy['prop'], $orderBy['order']);
// 3:每页大小查询
$pageSize = $pageSize ?: config('admin.defaultPageSize');
// 4:分页查询
$data = $builder->paginate($pageSize);
// 获取列表
return $data ? $data->toArray() : [];
}
}
~~~
- Laravel下载
- 项目管理
- Manager
- Vip专属链接管理
- Api
- Vip专属链接管理(Api)
- php artisan route:list 路由显示不全
- 数据迁移和填充
- Laravel5.5事件监听机制(注册-监听-任务调度)
- 章节1:未启用任务调度
- 章节2:启用任务调度
- 使用记录
- 数据迁移使用记录
- 安装laravel5.5日志观察器
- Laravel5.5消息队列(rabbitmq)
- 1:laravel自带消息队列
- 2:RabbitMq队列使用
- 第三方支付扩展:yansongda/laravel-pay
- 安装指引
- 控制器内使用
- 分表查询(mysql+mongo)
- 前端Vue按钮导出问题
- 单元测试
- 模型使用
- laravel9数据填充
- laravel9子查询
- Laravel反射机制
- Laravel删除文件后,重新加载文件配置,执行命令