Lae/app/Models/User.php

84 lines
2.2 KiB
PHP
Raw Normal View History

2022-08-12 07:56:56 +00:00
<?php
namespace App\Models;
2022-11-06 11:28:22 +00:00
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
2022-08-12 07:56:56 +00:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
2022-11-26 13:52:30 +00:00
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2022-11-18 09:16:30 +00:00
use Illuminate\Database\Eloquent\Relations\HasMany;
2022-11-06 11:28:22 +00:00
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
2022-08-12 07:56:56 +00:00
2022-11-06 11:28:22 +00:00
class User extends Authenticatable
2022-08-12 07:56:56 +00:00
{
2022-11-06 11:28:22 +00:00
use HasApiTokens, HasFactory, Notifiable, Cachable;
2022-08-12 07:56:56 +00:00
/**
* The attributes that are mass assignable.
*
2022-11-06 11:28:22 +00:00
* @var array<int, string>
2022-08-12 07:56:56 +00:00
*/
protected $fillable = [
'uuid',
2022-08-12 07:56:56 +00:00
'name',
'email',
'password',
];
/**
2022-11-06 11:28:22 +00:00
* The attributes that should be hidden for serialization.
2022-08-12 07:56:56 +00:00
*
2022-11-06 11:28:22 +00:00
* @var array<int, string>
2022-08-12 07:56:56 +00:00
*/
protected $hidden = [
2022-11-06 11:28:22 +00:00
'password',
'remember_token',
2022-08-12 07:56:56 +00:00
];
protected $casts = [
'email_verified_at' => 'datetime',
2022-11-30 12:26:35 +00:00
'balance' => 'decimal:2',
2022-09-13 11:24:01 +00:00
'banned_at' => 'datetime',
2022-12-30 13:32:04 +00:00
'birthday_at' => 'date',
2022-08-12 07:56:56 +00:00
];
2022-08-30 09:20:45 +00:00
protected static function boot()
{
parent::boot();
static::creating(function (self $user) {
$user->email_md5 = md5($user->email);
});
static::updating(function ($model) {
if ($model->isDirty('banned_at')) {
if ($model->banned_at) {
$model->tokens()->delete();
$model->hosts()->update(['status' => 'suspended', 'suspended_at' => now()]);
} else {
$model->hosts()->update(['status' => 'stopped']);
}
}
});
}
2023-01-05 14:12:47 +00:00
public function hosts(): HasMany
{
return $this->hasMany(Host::class);
}
public function user_group(): BelongsTo
{
return $this->belongsTo(UserGroup::class);
}
public function scopeBirthday()
{
2023-01-10 13:42:27 +00:00
/** @noinspection PhpUndefinedMethodInspection */
2023-01-05 14:12:47 +00:00
return $this->select(['id', 'name', 'birthday_at', 'email_md5', 'created_at'])->whereMonth('birthday_at', now()->month)
2023-01-10 14:36:49 +00:00
->whereDay('birthday_at', now()->day)->whereNull('banned_at');
2023-01-05 14:12:47 +00:00
}
2022-08-12 07:56:56 +00:00
}