1 | <?php |
||
16 | trait Tenantable |
||
17 | { |
||
18 | /** |
||
19 | * Register a saved model event with the dispatcher. |
||
20 | * |
||
21 | * @param \Closure|string $callback |
||
22 | * |
||
23 | * @return void |
||
24 | */ |
||
25 | abstract public static function saved($callback); |
||
26 | |||
27 | /** |
||
28 | * Register a deleted model event with the dispatcher. |
||
29 | * |
||
30 | * @param \Closure|string $callback |
||
31 | * |
||
32 | * @return void |
||
33 | */ |
||
34 | abstract public static function deleted($callback); |
||
35 | |||
36 | /** |
||
37 | * Define a polymorphic many-to-many relationship. |
||
38 | * |
||
39 | * @param string $related |
||
40 | * @param string $name |
||
41 | * @param string $table |
||
|
|||
42 | * @param string $foreignPivotKey |
||
43 | * @param string $relatedPivotKey |
||
44 | * @param string $parentKey |
||
45 | * @param string $relatedKey |
||
46 | * @param bool $inverse |
||
47 | * |
||
48 | * @return \Illuminate\Database\Eloquent\Relations\MorphToMany |
||
49 | */ |
||
50 | abstract public function morphToMany( |
||
60 | |||
61 | /** |
||
62 | * Get all attached tenants to the model. |
||
63 | * |
||
64 | * @return \Illuminate\Database\Eloquent\Relations\MorphToMany |
||
65 | */ |
||
66 | public function tenants(): MorphToMany |
||
71 | |||
72 | /** |
||
73 | * Attach the given tenant(s) to the model. |
||
74 | * |
||
75 | * @param mixed $tenants |
||
76 | * |
||
77 | * @return void |
||
78 | */ |
||
79 | public function setTenantsAttribute($tenants): void |
||
85 | |||
86 | /** |
||
87 | * Boot the tenantable trait for the model. |
||
88 | * |
||
89 | * @return void |
||
90 | */ |
||
91 | public static function bootTenantable() |
||
92 | { |
||
93 | if (app()->has('request.tenant') && $tenant = app('request.tenant')) { |
||
94 | static::addGlobalScope('tenantable', function (Builder $builder) use ($tenant) { |
||
95 | $builder->whereHas('tenants', function (Builder $builder) use ($tenant) { |
||
96 | $key = $tenant instanceof Model ? $tenant->getKeyName() : (is_int($tenant) ? 'id' : 'slug'); |
||
97 | $value = $tenant instanceof Model ? $tenant->{$key} : $tenant; |
||
98 | $builder->where($key, $value); |
||
99 | }); |
||
100 | }); |
||
101 | |||
102 | static::saved(function (self $model) use ($tenant) { |
||
103 | $model->attachTenants($tenant); |
||
104 | }); |
||
105 | } |
||
106 | |||
107 | static::deleted(function (self $model) { |
||
108 | $model->tenants()->detach(); |
||
109 | }); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Returns a new query builder without any of the tenant scopes applied. |
||
114 | * |
||
115 | * @return \Illuminate\Database\Eloquent\Builder |
||
116 | */ |
||
117 | public static function forAllTenants() |
||
121 | |||
122 | /** |
||
123 | * Override the default findOrFail method so that we can re-throw |
||
124 | * a more useful exception. Otherwise it can be very confusing |
||
125 | * why queries don't work because of tenant scoping issues. |
||
126 | * |
||
127 | * @param mixed $id |
||
128 | * @param array $columns |
||
129 | * |
||
130 | * @throws \Rinvex\Tenants\Exceptions\ModelNotFoundForTenantException |
||
131 | * |
||
132 | * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection |
||
133 | */ |
||
134 | public static function findOrFail($id, $columns = ['*']) |
||
147 | |||
148 | /** |
||
149 | * Scope query with all the given tenants. |
||
150 | * |
||
151 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
152 | * @param mixed $tenants |
||
153 | * |
||
154 | * @return \Illuminate\Database\Eloquent\Builder |
||
155 | */ |
||
156 | public function scopeWithAllTenants(Builder $builder, $tenants): Builder |
||
168 | |||
169 | /** |
||
170 | * Scope query with any of the given tenants. |
||
171 | * |
||
172 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
173 | * @param mixed $tenants |
||
174 | * |
||
175 | * @return \Illuminate\Database\Eloquent\Builder |
||
176 | */ |
||
177 | public function scopeWithAnyTenants(Builder $builder, $tenants): Builder |
||
185 | |||
186 | /** |
||
187 | * Scope query with any of the given tenants. |
||
188 | * |
||
189 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
190 | * @param mixed $tenants |
||
191 | * |
||
192 | * @return \Illuminate\Database\Eloquent\Builder |
||
193 | */ |
||
194 | public function scopeWithTenants(Builder $builder, $tenants): Builder |
||
198 | |||
199 | /** |
||
200 | * Scope query without any of the given tenants. |
||
201 | * |
||
202 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
203 | * @param mixed $tenants |
||
204 | * |
||
205 | * @return \Illuminate\Database\Eloquent\Builder |
||
206 | */ |
||
207 | public function scopeWithoutTenants(Builder $builder, $tenants): Builder |
||
215 | |||
216 | /** |
||
217 | * Scope query without any tenants. |
||
218 | * |
||
219 | * @param \Illuminate\Database\Eloquent\Builder $builder |
||
220 | * |
||
221 | * @return \Illuminate\Database\Eloquent\Builder |
||
222 | */ |
||
223 | public function scopeWithoutAnyTenants(Builder $builder): Builder |
||
227 | |||
228 | /** |
||
229 | * Determine if the model has any of the given tenants. |
||
230 | * |
||
231 | * @param mixed $tenants |
||
232 | * |
||
233 | * @return bool |
||
234 | */ |
||
235 | public function hasTenants($tenants): bool |
||
241 | |||
242 | /** |
||
243 | * Determine if the model has any the given tenants. |
||
244 | * |
||
245 | * @param mixed $tenants |
||
246 | * |
||
247 | * @return bool |
||
248 | */ |
||
249 | public function hasAnyTenants($tenants): bool |
||
253 | |||
254 | /** |
||
255 | * Determine if the model has all of the given tenants. |
||
256 | * |
||
257 | * @param mixed $tenants |
||
258 | * |
||
259 | * @return bool |
||
260 | */ |
||
261 | public function hasAllTenants($tenants): bool |
||
267 | |||
268 | /** |
||
269 | * Sync model tenants. |
||
270 | * |
||
271 | * @param mixed $tenants |
||
272 | * @param bool $detaching |
||
273 | * |
||
274 | * @return $this |
||
275 | */ |
||
276 | public function syncTenants($tenants, bool $detaching = true) |
||
286 | |||
287 | /** |
||
288 | * Attach model tenants. |
||
289 | * |
||
290 | * @param mixed $tenants |
||
291 | * |
||
292 | * @return $this |
||
293 | */ |
||
294 | public function attachTenants($tenants) |
||
298 | |||
299 | /** |
||
300 | * Detach model tenants. |
||
301 | * |
||
302 | * @param mixed $tenants |
||
303 | * |
||
304 | * @return $this |
||
305 | */ |
||
306 | public function detachTenants($tenants = null) |
||
315 | |||
316 | /** |
||
317 | * Prepare tenant IDs. |
||
318 | * |
||
319 | * @param mixed $tenants |
||
320 | * |
||
321 | * @return array |
||
322 | */ |
||
323 | protected function prepareTenantIds($tenants): array |
||
354 | } |
||
355 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.