1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SebastianBerc\Repositories\Traits; |
4
|
|
|
|
5
|
|
|
use DB; |
6
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
7
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
8
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; |
9
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOne; |
10
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOneOrMany; |
11
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Sortable. |
15
|
|
|
* |
16
|
|
|
* @author Sebastian Berć <[email protected]> |
17
|
|
|
* @copyright Copyright (c) Sebastian Berć |
18
|
|
|
*/ |
19
|
|
|
trait Sortable |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Append many column sorting to query builder. |
23
|
|
|
* |
24
|
|
|
* @param array $columns |
25
|
|
|
* |
26
|
|
|
* @return $this |
27
|
|
|
*/ |
28
|
16 |
|
public function multiSortBy(array $columns) |
29
|
|
|
{ |
30
|
16 |
|
foreach ($columns as $column => $direction) { |
31
|
4 |
|
if (strpos($column, '.')) { |
32
|
2 |
|
$this->sortByRelation($column, $direction); |
|
|
|
|
33
|
2 |
|
} else { |
34
|
2 |
|
$this->sortBy($column, $direction); |
35
|
|
|
} |
36
|
16 |
|
} |
37
|
|
|
|
38
|
16 |
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Append relation column sorting to query builder. |
43
|
|
|
* |
44
|
|
|
* @param array $column |
45
|
|
|
* @param string $direction |
46
|
|
|
* |
47
|
|
|
* @return $this |
48
|
|
|
*/ |
49
|
2 |
|
public function sortByRelation($column, $direction = 'ASC') |
50
|
|
|
{ |
51
|
2 |
|
$relations = explode('.', $column); |
52
|
2 |
|
$column = array_pop($relations); |
53
|
|
|
|
54
|
|
|
/** @var \Illuminate\Database\Eloquent\Model $model */ |
55
|
2 |
|
$model = $this->repository->makeModel(); |
|
|
|
|
56
|
|
|
|
57
|
2 |
|
$this->instance->getQuery()->orders = []; |
|
|
|
|
58
|
|
|
|
59
|
2 |
|
foreach ($relations as $relation) { |
60
|
|
|
/* @var Relation $relationClass */ |
61
|
2 |
|
$this->joinRelation($relationClass = $model->{camel_case($relation)}()); |
62
|
|
|
|
63
|
2 |
|
$model = $relationClass->getRelated(); |
64
|
2 |
|
} |
65
|
|
|
|
66
|
2 |
|
$this->instance->select(DB::raw("{$this->repository->makeModel()->getTable()}.*")) |
67
|
2 |
|
->orderBy("{$model->getTable()}.{$column}", $direction); |
68
|
|
|
|
69
|
2 |
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Add joining the tables to query based on the type of relationship. |
74
|
|
|
* |
75
|
|
|
* @param Relation $relationClass |
76
|
|
|
* |
77
|
|
|
* @return $this |
78
|
|
|
*/ |
79
|
2 |
|
protected function joinRelation(Relation $relationClass) |
80
|
|
|
{ |
81
|
2 |
|
switch (get_class($relationClass)) { |
82
|
2 |
|
case BelongsToMany::class: |
83
|
|
|
/* @var BelongsToMany $relationClass */ |
84
|
|
|
$this->joinBelongsToMany($relationClass); |
85
|
|
|
break; |
86
|
2 |
|
case BelongsTo::class: |
87
|
|
|
/* @var BelongsTo $relationClass */ |
88
|
2 |
|
$this->joinBelongsTo($relationClass); |
89
|
2 |
|
break; |
90
|
2 |
|
case HasOne::class: |
91
|
2 |
|
case HasMany::class: |
92
|
|
|
/* @var HasOneOrMany $relationClass */ |
93
|
2 |
|
$this->joinHasOneOrMany($relationClass); |
94
|
2 |
|
break; |
95
|
2 |
|
} |
96
|
2 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Join a belongs to many relationship. |
100
|
|
|
* |
101
|
|
|
* @param BelongsToMany $relation |
102
|
|
|
* |
103
|
|
|
* @return mixed |
104
|
|
|
*/ |
105
|
|
|
protected function joinBelongsToMany(BelongsToMany $relation) |
106
|
|
|
{ |
107
|
|
|
return $this->instance->join( |
108
|
|
|
$relation->getTable(), |
109
|
|
|
$relation->getParent()->getTable() . '.' . $relation->getParent()->getKeyName(), |
110
|
|
|
'=', |
111
|
|
|
$relation->getForeignKey() |
112
|
|
|
)->join( |
113
|
|
|
$relation->getRelated()->getTable(), |
114
|
|
|
$relation->getRelated()->getTable() . '.' . $relation->getRelated()->getKeyName(), |
115
|
|
|
'=', |
116
|
|
|
$relation->getOtherKey() |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Join a belongs to relationship. |
122
|
|
|
* |
123
|
|
|
* @param BelongsTo $relation |
124
|
|
|
* |
125
|
|
|
* @return mixed |
126
|
|
|
*/ |
127
|
2 |
|
protected function joinBelongsTo(BelongsTo $relation) |
128
|
|
|
{ |
129
|
2 |
|
return $this->instance->join( |
130
|
2 |
|
$relation->getRelated()->getTable(), |
131
|
2 |
|
$relation->getQualifiedOtherKeyName(), |
132
|
2 |
|
'=', |
133
|
2 |
|
$relation->getQualifiedForeignKey() |
134
|
2 |
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Join a has one relationship. |
139
|
|
|
* |
140
|
|
|
* @param HasOneOrMany $relation |
141
|
|
|
* |
142
|
|
|
* @return mixed |
143
|
|
|
*/ |
144
|
2 |
|
protected function joinHasOneOrMany(HasOneOrMany $relation) |
145
|
|
|
{ |
146
|
2 |
|
return $this->instance->join( |
147
|
2 |
|
$relation->getRelated()->getTable(), |
148
|
2 |
|
$relation->getRelated()->getTable() . '.' . $relation->getParent()->getForeignKey(), |
149
|
2 |
|
'=', |
150
|
2 |
|
$relation->getParent()->getTable() . '.' . $relation->getParent()->getKeyName() |
151
|
2 |
|
); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Append column sorting to query builder. |
156
|
|
|
* |
157
|
|
|
* @param string|array $column |
158
|
|
|
* @param string $direction |
159
|
|
|
* |
160
|
|
|
* @return $this |
161
|
|
|
*/ |
162
|
2 |
|
public function sortBy($column, $direction = 'ASC') |
163
|
|
|
{ |
164
|
2 |
|
$this->instance->orderBy($column, $direction); |
165
|
|
|
|
166
|
2 |
|
return $this; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: