~~~
<?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() : [];
}
}
~~~
- Laravel5.5總結
- 項目管理
- 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子查詢