0722cf3120
git commit "implement user management with search sort permissions and safe delete"
135 lines
2.7 KiB
PHP
135 lines
2.7 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Created by Reliese Model.
|
|
*/
|
|
|
|
namespace App\Models;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Attributes\Scope;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
|
|
/**
|
|
* Class User
|
|
*
|
|
* @property int $id
|
|
* @property string $first_name
|
|
* @property string $last_name
|
|
* @property string $national_code
|
|
* @property int $gender
|
|
* @property int $marital_status
|
|
* @property string|null $spouse_name
|
|
* @property string $mobile
|
|
* @property string $email
|
|
* @property string|null $profile_image
|
|
* @property string $username
|
|
* @property string $password
|
|
* @property int $province_id
|
|
* @property int $city_id
|
|
* @property int $status
|
|
* @property Carbon $created_at
|
|
* @property Carbon $updated_at
|
|
*
|
|
* @property City $city
|
|
* @property Collection|Comment[] $comments
|
|
* @property Collection|Favorite[] $favorites
|
|
*
|
|
* @package App\Models
|
|
*/
|
|
class User extends Authenticatable
|
|
{
|
|
protected $table = 'users';
|
|
public static $snakeAttributes = false;
|
|
|
|
protected $casts = [
|
|
'gender' => 'int',
|
|
'marital_status' => 'int',
|
|
'province_id' => 'int',
|
|
'city_id' => 'int',
|
|
'status' => 'int'
|
|
];
|
|
|
|
protected $hidden = [
|
|
'password'
|
|
];
|
|
|
|
protected $fillable = [
|
|
'first_name',
|
|
'last_name',
|
|
'national_code',
|
|
'gender',
|
|
'marital_status',
|
|
'spouse_name',
|
|
'mobile',
|
|
'email',
|
|
'profile_image',
|
|
'username',
|
|
'password',
|
|
'province_id',
|
|
'city_id',
|
|
'status'
|
|
];
|
|
#[Scope]
|
|
protected function applySearch(Builder $query): void
|
|
{
|
|
$request = request();
|
|
|
|
if ($request->filled('keyword')) {
|
|
$keyword = $request->input('keyword');
|
|
|
|
$query->whereAny([
|
|
'first_name',
|
|
'last_name',
|
|
'mobile',
|
|
'username',
|
|
], 'LIKE', "%$keyword%");
|
|
}
|
|
}
|
|
|
|
#[Scope]
|
|
protected function applySort(Builder $query): void
|
|
{
|
|
$sort = request('sort');
|
|
|
|
match ($sort) {
|
|
|
|
'first_name_asc' =>
|
|
$query->orderBy('first_name', 'asc'),
|
|
|
|
'first_name_desc' =>
|
|
$query->orderBy('first_name', 'desc'),
|
|
|
|
'newest' =>
|
|
$query->orderBy('created_at', 'desc'),
|
|
|
|
default =>
|
|
$query->orderBy('created_at', 'desc'),
|
|
};
|
|
}
|
|
public function city()
|
|
{
|
|
return $this->belongsTo(City::class);
|
|
}
|
|
public function province()
|
|
{
|
|
return $this->belongsTo(Province::class);
|
|
}
|
|
|
|
public function comments()
|
|
{
|
|
return $this->hasMany(Comment::class);
|
|
}
|
|
|
|
public function favorites()
|
|
{
|
|
return $this->hasMany(Favorite::class);
|
|
}
|
|
public function reservations()
|
|
{
|
|
return $this->hasMany(Reservation::class);
|
|
}
|
|
}
|