These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Spatie\QueryBuilder\Concerns; |
||
4 | |||
5 | use Illuminate\Database\Eloquent\Model; |
||
6 | use Illuminate\Support\Collection; |
||
7 | use Spatie\QueryBuilder\Exceptions\InvalidAppendQuery; |
||
8 | |||
9 | trait AppendsAttributesToResults |
||
10 | { |
||
11 | /** @var \Illuminate\Support\Collection */ |
||
12 | protected $allowedAppends; |
||
13 | |||
14 | public function allowedAppends($appends): self |
||
15 | { |
||
16 | $appends = is_array($appends) ? $appends : func_get_args(); |
||
17 | |||
18 | $this->allowedAppends = collect($appends); |
||
19 | |||
20 | $this->ensureAllAppendsExist(); |
||
21 | |||
22 | return $this; |
||
23 | } |
||
24 | |||
25 | protected function addAppendsToResults(Collection $results) |
||
26 | { |
||
27 | return $results->each(function (Model $result) { |
||
28 | return $result->append($this->request->appends()->toArray()); |
||
0 ignored issues
–
show
|
|||
29 | }); |
||
30 | } |
||
31 | |||
32 | protected function ensureAllAppendsExist() |
||
33 | { |
||
34 | $appends = $this->request->appends(); |
||
35 | |||
36 | $diff = $appends->diff($this->allowedAppends); |
||
37 | |||
38 | if ($diff->count()) { |
||
39 | throw InvalidAppendQuery::appendsNotAllowed($diff, $this->allowedAppends); |
||
40 | } |
||
41 | } |
||
42 | } |
||
43 |
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: