1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Xetaravel\Models; |
||
6 | |||
7 | use Illuminate\Database\Eloquent\Factories\HasFactory; |
||
8 | use Illuminate\Database\Eloquent\Relations\BelongsTomany; |
||
9 | |||
10 | class Badge extends Model |
||
11 | { |
||
12 | use HasFactory; |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
13 | |||
14 | /** |
||
15 | * The attributes that are mass assignable. |
||
16 | * |
||
17 | * @var array |
||
18 | */ |
||
19 | protected $fillable = [ |
||
20 | 'name', |
||
21 | 'description', |
||
22 | 'icon', |
||
23 | 'color', |
||
24 | 'type', |
||
25 | 'rule' |
||
26 | ]; |
||
27 | |||
28 | /** |
||
29 | * Get the users that owns the badge. |
||
30 | * |
||
31 | * @return BelongsTomany |
||
32 | */ |
||
33 | public function users(): BelongsTomany |
||
34 | { |
||
35 | return $this->belongsToMany(User::class)->withTrashed(); |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Check if the given user has unlocked this badge. |
||
40 | * |
||
41 | * @param User $user The user to check. |
||
42 | * |
||
43 | * @return bool |
||
44 | */ |
||
45 | public function hasUser(User $user): bool |
||
46 | { |
||
47 | return $this->users() |
||
48 | ->where('user_id', $user->getKey()) |
||
49 | ->exists(); |
||
50 | } |
||
51 | } |
||
52 |