45 lines
1.8 KiB
PHP
45 lines
1.8 KiB
PHP
<?php
|
|
namespace App\Models;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
use Illuminate\Notifications\Notifiable;
|
|
|
|
class User extends Authenticatable
|
|
{
|
|
use HasFactory, Notifiable;
|
|
|
|
protected $fillable = ['name','slug','email','password','avatar','bio','location','website','is_private','is_admin'];
|
|
protected $hidden = ['password','remember_token'];
|
|
protected $casts = ['email_verified_at'=>'datetime','password'=>'hashed','is_private'=>'boolean','is_admin'=>'boolean'];
|
|
|
|
public function getRouteKeyName(): string { return 'slug'; }
|
|
|
|
public function library(): HasMany { return $this->hasMany(UserGame::class); }
|
|
public function reviews(): HasMany { return $this->hasMany(Review::class); }
|
|
public function lists(): HasMany { return $this->hasMany(UserList::class); }
|
|
public function activities(): HasMany { return $this->hasMany(Activity::class)->latest(); }
|
|
public function notifications(): HasMany { return $this->hasMany(Notification::class)->latest('created_at'); }
|
|
|
|
public function following(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(User::class, 'follows', 'follower_id', 'following_id')->withTimestamps();
|
|
}
|
|
|
|
public function followers(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(User::class, 'follows', 'following_id', 'follower_id')->withTimestamps();
|
|
}
|
|
|
|
public function isFollowing(User $user): bool
|
|
{
|
|
return $this->following()->where('following_id', $user->id)->exists();
|
|
}
|
|
|
|
public function gameEntry(int $gameId): ?UserGame
|
|
{
|
|
return $this->library()->where('game_id', $gameId)->first();
|
|
}
|
|
}
|