kenken999/php
0
1<?php2 3namespace App\Models;4 5use Illuminate\Database\Eloquent\Factories\HasFactory;6use Illuminate\Database\Eloquent\Relations\BelongsToMany;7use Illuminate\Database\Eloquent\Relations\HasMany;8use Illuminate\Foundation\Auth\User as Authenticatable;9use Tymon\JWTAuth\Contracts\JWTSubject;10 11class User extends Authenticatable implements JWTSubject12{13 use HasFactory;14 15 protected $fillable = ['username', 'email', 'password', 'bio', 'images'];16 17 protected $visible = ['username', 'email', 'bio', 'images'];18 19 public function getRouteKeyName(): string20 {21 return 'username';22 }23 24 public function articles(): HasMany25 {26 return $this->hasMany(Article::class);27 }28 29 public function favoritedArticles(): BelongsToMany30 {31 return $this->belongsToMany(Article::class);32 }33 34 public function followers(): BelongsToMany35 {36 return $this->belongsToMany(User::class, 'followers', 'following_id', 'follower_id');37 }38 39 public function following(): BelongsToMany40 {41 return $this->belongsToMany(User::class, 'followers', 'follower_id', 'following_id');42 }43 44 public function doesUserFollowAnotherUser(int $followerId, int $followingId): bool45 {46 return $this->where('id', $followerId)->whereRelation('following', 'id', $followingId)->exists();47 }48 49 public function doesUserFollowArticle(int $userId, int $articleId): bool50 {51 return $this->where('id', $userId)->whereRelation('favoritedArticles', 'id', $articleId)->exists();52 }53 54 public function setPasswordAttribute(string $password): void55 {56 $this->attributes['password'] = bcrypt($password);57 }58 59 public function getJWTIdentifier()60 {61 return $this->getKey();62 }63 64 public function getJWTCustomClaims()65 {66 return [];67 }68}69 