12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- <?php
- namespace App\Models;
- use App\Constants\HttpStatus;
- use Illuminate\Contracts\Auth\Authenticatable;
- use Illuminate\Database\Eloquent\Model;
- use Exception;
- use Illuminate\Database\Eloquent\Builder;
- /**
- * Admin
- * @mixin Builder
- * @method static Builder|static where($column, $operator = null, $value = null, $boolean = 'and')
- */
- class Admin extends Model implements Authenticatable
- {
- use \Illuminate\Auth\Authenticatable; // 使用 Auth 认证相关功能
- protected $table = 'admin';
- protected $hidden = ['password', 'created_at', 'updated_at'];
- protected $fillable = ['username','nickname','password','sex','cellphone','email','remarks', 'created_at', 'updated_at'];
- protected $appends = ['roles_ids', 'roles_names'];
- public static function login($username, $password)
- {
- $map = [];
- $map[] = ['username', '=', $username];
- $user = new Admin();
- $user = $user->where($map)->first();
- if (!$user) throw new Exception('', HttpStatus::USER_DOES_NOT_EXIST);
- if (!password_verify($password, $user->password)) throw new Exception('', HttpStatus::PASSWORDS_ERROR);
- return $user;
- }
- public function roles()
- {
- return $this->belongsToMany(Role::class, 'role_user', 'user_id', 'role_id');
- }
- public function getRolesIdsAttribute()
- {
- return $this->roles->pluck('id')->toArray();
- }
- public function getRolesNamesAttribute()
- {
- return $this->roles->pluck('display_name')->toArray();
- }
- }
|