1 | <?php |
||
11 | trait AddsIncludesToQuery |
||
12 | { |
||
13 | /** @var \Illuminate\Support\Collection */ |
||
14 | protected $allowedIncludes; |
||
15 | |||
16 | public function allowedIncludes($includes): self |
||
17 | { |
||
18 | $includes = is_array($includes) ? $includes : func_get_args(); |
||
19 | |||
20 | $this->allowedIncludes = collect($includes) |
||
21 | ->reject(function ($include) { |
||
22 | return empty($include); |
||
23 | }) |
||
24 | ->flatMap(function ($include): Collection { |
||
25 | if ($include instanceof IncludeInterface) { |
||
26 | return collect([$include]); |
||
27 | } |
||
28 | |||
29 | if (Str::endsWith($include, config('query-builder.count_suffix'))) { |
||
30 | return AllowedInclude::count($include); |
||
31 | } |
||
32 | |||
33 | return AllowedInclude::relationship($include); |
||
34 | }) |
||
35 | ->unique(function (AllowedInclude $allowedInclude) { |
||
36 | return $allowedInclude->getName(); |
||
37 | }); |
||
38 | |||
39 | if ($this->ensureAllIncludesExist()) { |
||
40 | $this->addIncludesToQuery($this->request->includes()); |
||
|
|||
41 | } |
||
42 | |||
43 | return $this; |
||
44 | } |
||
45 | |||
46 | protected function addIncludesToQuery(Collection $includes) |
||
54 | |||
55 | protected function findInclude(string $include): ?AllowedInclude |
||
62 | |||
63 | protected function ensureAllIncludesExist(): bool |
||
85 | } |
||
86 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: