|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NavJobs\Transmit\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
|
6
|
|
|
|
|
7
|
|
|
trait QueryHelperTrait { |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Eager loads the provided includes on the specified model. |
|
11
|
|
|
* If the class has a fractal instance, will also use include params. |
|
12
|
|
|
* |
|
13
|
|
|
* @param $model |
|
14
|
|
|
* @param $includes |
|
15
|
|
|
* @return mixed |
|
16
|
|
|
*/ |
|
17
|
|
|
protected function eagerLoadIncludes($model, $includes) |
|
18
|
|
|
{ |
|
19
|
|
|
$builder = $model; |
|
20
|
|
|
|
|
21
|
|
|
foreach ($includes as $include) { |
|
22
|
|
|
if (method_exists($model, $include) && $model->$include() instanceof Relation) { |
|
23
|
|
|
$builder = $builder->with([ |
|
24
|
|
|
$include => function ($query) use ($include) { |
|
25
|
|
|
$parameters = $this->fractal ? $this->fractal->getIncludeParams($include) : null; |
|
|
|
|
|
|
26
|
|
|
$this->applyParameters($query, $parameters); |
|
27
|
|
|
} |
|
28
|
|
|
]); |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
return $builder; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Apply query parameters to the supplied query builder. |
|
37
|
|
|
* |
|
38
|
|
|
* @param $builder |
|
39
|
|
|
* @param Symfony\Component\HttpFoundation\ParameterBag|League\Fractal\ParamBag $parameters |
|
40
|
|
|
* @return mixed |
|
41
|
|
|
*/ |
|
42
|
|
|
protected function applyParameters($builder, $parameters = null) |
|
43
|
|
|
{ |
|
44
|
|
|
if (!$parameters) { |
|
45
|
|
|
return $builder; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if ($parameters->get('sort')) { |
|
49
|
|
|
$builder = $this->sortBuilder($builder, $parameters); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
if ($parameters->get('limit')) { |
|
54
|
|
|
$builder = $this->limitBuilder($builder, $parameters); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $builder; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Applies sorts to the Builder. |
|
62
|
|
|
* |
|
63
|
|
|
* @param $builder |
|
64
|
|
|
* @param $parameters |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function sortBuilder($builder, $parameters) |
|
67
|
|
|
{ |
|
68
|
|
|
$sorts = explode(',', str_replace('|', ',', $parameters->get('sort'))); |
|
69
|
|
|
|
|
70
|
|
|
foreach ($sorts as $sort) { |
|
71
|
|
|
$sortDirection = str_contains($sort, '-') ? 'desc' : 'asc'; |
|
72
|
|
|
$sortColumn = ltrim($sort, '-'); |
|
73
|
|
|
|
|
74
|
|
|
$builder = $builder->orderBy($sortColumn, $sortDirection); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $builder; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Applies limits to the Builder. |
|
82
|
|
|
* |
|
83
|
|
|
* @param $builder |
|
84
|
|
|
* @param $parameters |
|
85
|
|
|
*/ |
|
86
|
|
|
public function limitBuilder($builder, $parameters) |
|
87
|
|
|
{ |
|
88
|
|
|
if (is_a($builder, Relation::class)) { |
|
89
|
|
|
return $builder; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $builder->take($parameters->get('limit'))->skip($parameters->get('offset')); |
|
93
|
|
|
} |
|
94
|
|
|
} |
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: