1 | <?php |
||
11 | trait AddsFieldsToQuery |
||
12 | { |
||
13 | /** @var \Illuminate\Support\Collection */ |
||
14 | protected $allowedFields; |
||
15 | |||
16 | public function allowedFields($fields): self |
||
17 | { |
||
18 | if ($this->allowedIncludes instanceof Collection) { |
||
|
|||
19 | throw new AllowedFieldsMustBeCalledBeforeAllowedIncludes(); |
||
20 | } |
||
21 | |||
22 | $fields = is_array($fields) ? $fields : func_get_args(); |
||
23 | |||
24 | $this->allowedFields = collect($fields) |
||
25 | ->map(function (string $fieldName) { |
||
26 | return $this->prependField($fieldName); |
||
27 | }); |
||
28 | |||
29 | if ($this->ensureAllFieldsExist()) { |
||
30 | $this->addRequestedModelFieldsToQuery(); |
||
31 | } |
||
32 | |||
33 | return $this; |
||
34 | } |
||
35 | |||
36 | protected function addRequestedModelFieldsToQuery() |
||
50 | |||
51 | public function getRequestedFieldsForRelatedTable(string $relation): array |
||
73 | |||
74 | protected function ensureAllFieldsExist(): bool |
||
99 | |||
100 | protected function prependFieldsWithTableName(array $fields, string $tableName): array |
||
106 | |||
107 | protected function prependField(string $field, ?string $table = null): string |
||
121 | } |
||
122 |
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: