1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Siak\Tontine\Model; |
4
|
|
|
|
5
|
|
|
use Database\Factories\MemberFactory; |
6
|
|
|
use Illuminate\Database\Eloquent\Factories\Factory; |
7
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory; |
8
|
|
|
use Illuminate\Database\Eloquent\Builder; |
9
|
|
|
use Illuminate\Support\Facades\DB; |
10
|
|
|
|
11
|
|
|
class Member extends Base |
12
|
|
|
{ |
13
|
|
|
use HasFactory; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Indicates if the model should be timestamped. |
17
|
|
|
* |
18
|
|
|
* @var bool |
19
|
|
|
*/ |
20
|
|
|
public $timestamps = false; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The attributes that are mass assignable. |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $fillable = [ |
28
|
|
|
'name', |
29
|
|
|
'email', |
30
|
|
|
'phone', |
31
|
|
|
'address', |
32
|
|
|
'city', |
33
|
|
|
'registered_at', |
34
|
|
|
'birthday', |
35
|
|
|
'active', |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get the attributes that should be cast. |
40
|
|
|
* |
41
|
|
|
* @return array<string, string> |
42
|
|
|
*/ |
43
|
|
|
protected function casts(): array |
44
|
|
|
{ |
45
|
|
|
return [ |
46
|
|
|
'registered_at' => 'datetime', |
47
|
|
|
'birthday' => 'datetime', |
48
|
|
|
]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Create a new factory instance for the model. |
53
|
|
|
* |
54
|
|
|
* @return Factory |
55
|
|
|
*/ |
56
|
|
|
protected static function newFactory() |
57
|
|
|
{ |
58
|
|
|
return MemberFactory::new(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function guild() |
62
|
|
|
{ |
63
|
|
|
return $this->belongsTo(Guild::class); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function subscriptions() |
67
|
|
|
{ |
68
|
|
|
return $this->hasMany(Subscription::class); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function loans() |
72
|
|
|
{ |
73
|
|
|
return $this->hasMany(Loan::class); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function libre_bills() |
77
|
|
|
{ |
78
|
|
|
return $this->hasMany(LibreBill::class); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function session_bills() |
82
|
|
|
{ |
83
|
|
|
return $this->hasMany(SessionBill::class); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function round_bills() |
87
|
|
|
{ |
88
|
|
|
return $this->hasMany(RoundBill::class); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function oneoff_bills() |
92
|
|
|
{ |
93
|
|
|
return $this->hasMany(OneoffBill::class); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function savings() |
97
|
|
|
{ |
98
|
|
|
return $this->hasMany(Saving::class); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function absences() |
102
|
|
|
{ |
103
|
|
|
return $this->belongsToMany(Session::class, 'absences'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param Builder $query |
108
|
|
|
* |
109
|
|
|
* @return Builder |
110
|
|
|
*/ |
111
|
|
|
public function scopeActive(Builder $query): Builder |
112
|
|
|
{ |
113
|
|
|
return $query->where('active', true); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param Builder $query |
118
|
|
|
* @param string $search |
119
|
|
|
* |
120
|
|
|
* @return Builder |
121
|
|
|
*/ |
122
|
|
|
public function scopeSearch(Builder $query, string $search): Builder |
123
|
|
|
{ |
124
|
|
|
return $query |
125
|
|
|
->when($search !== '', fn($query) => $query |
126
|
|
|
->where(DB::raw('lower(name)'), 'like', "%{$search}%")); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|