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\HasOneOrMany; |
9
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Sortable. |
13
|
|
|
* |
14
|
|
|
* @author Sebastian Berć <[email protected]> |
15
|
|
|
* @copyright Copyright (c) Sebastian Berć |
16
|
|
|
*/ |
17
|
|
|
trait Sortable |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Append many column sorting to query builder. |
21
|
|
|
* |
22
|
|
|
* @param array $columns |
23
|
|
|
* |
24
|
|
|
* @return $this |
25
|
|
|
*/ |
26
|
14 |
|
public function multiSortBy(array $columns) |
27
|
|
|
{ |
28
|
14 |
|
foreach ($columns as $column => $direction) { |
29
|
4 |
|
if (strpos($column, '.')) { |
30
|
2 |
|
$this->sortByRelation($column, $direction); |
|
|
|
|
31
|
2 |
|
} else { |
32
|
2 |
|
$this->sortBy($column, $direction); |
33
|
|
|
} |
34
|
14 |
|
} |
35
|
|
|
|
36
|
14 |
|
return $this; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Append relation column sorting to query builder. |
41
|
|
|
* |
42
|
|
|
* @param array $column |
43
|
|
|
* @param string $direction |
44
|
|
|
* |
45
|
|
|
* @return $this |
46
|
|
|
*/ |
47
|
2 |
|
public function sortByRelation($column, $direction = 'ASC') |
48
|
|
|
{ |
49
|
2 |
|
$relations = explode('.', $column); |
50
|
2 |
|
$column = array_pop($relations); |
51
|
|
|
|
52
|
|
|
/** @var \Illuminate\Database\Eloquent\Model $model */ |
53
|
2 |
|
$model = $this->repository->makeModel(); |
|
|
|
|
54
|
|
|
|
55
|
2 |
|
$this->instance->getQuery()->orders = []; |
|
|
|
|
56
|
|
|
|
57
|
2 |
|
foreach ($relations as $relation) { |
58
|
|
|
/* @var Relation $relationClass */ |
59
|
2 |
|
$this->joinRelation($relationClass = $model->{camel_case($relation)}()); |
60
|
|
|
|
61
|
2 |
|
$model = $relationClass->getRelated(); |
62
|
2 |
|
} |
63
|
|
|
|
64
|
2 |
|
$this->instance->select(DB::raw("{$this->repository->makeModel()->getTable()}.*")) |
65
|
2 |
|
->orderBy("{$model->getTable()}.{$column}", $direction); |
66
|
|
|
|
67
|
2 |
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Add joining the tables to query based on the type of relationship. |
72
|
|
|
* |
73
|
|
|
* @param Relation $relationClass |
74
|
|
|
*/ |
75
|
2 |
|
protected function joinRelation(Relation $relationClass) |
76
|
|
|
{ |
77
|
2 |
|
switch (get_class($relationClass)) { |
78
|
2 |
|
case BelongsToMany::class: |
79
|
|
|
/* @var BelongsToMany $relationClass */ |
80
|
|
|
$this->instance = $this->joinBelongsToMany($relationClass); |
81
|
|
|
break; |
82
|
2 |
|
case BelongsTo::class: |
83
|
|
|
/* @var BelongsTo $relationClass */ |
84
|
|
|
$this->instance = $this->joinBelongsTo($relationClass); |
85
|
|
|
break; |
86
|
2 |
|
case HasOneOrMany::class: |
87
|
|
|
/* @var HasOneOrMany $relationClass */ |
88
|
|
|
$this->instance = $this->joinHasOne($relationClass); |
89
|
|
|
break; |
90
|
2 |
|
} |
91
|
2 |
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Join a belongs to many relationship. |
95
|
|
|
* |
96
|
|
|
* @param BelongsToMany $relation |
97
|
|
|
* |
98
|
|
|
* @return mixed |
99
|
|
|
*/ |
100
|
|
|
protected function joinBelongsToMany(BelongsToMany $relation) |
101
|
|
|
{ |
102
|
|
|
return $this->instance->join($relation->getTable(), |
103
|
|
|
$relation->getParent()->getTable() . '.' . $relation->getParent()->getKeyName(), '=', |
104
|
|
|
$relation->getForeignKey())->join($relation->getRelated()->getTable(), |
105
|
|
|
$relation->getRelated()->getTable() . '.' . $relation->getRelated()->getKeyName(), '=', |
106
|
|
|
$relation->getOtherKey()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Join a belongs to relationship. |
111
|
|
|
* |
112
|
|
|
* @param BelongsTo $relation |
113
|
|
|
* |
114
|
|
|
* @return mixed |
115
|
|
|
*/ |
116
|
|
|
protected function joinBelongsTo(BelongsTo $relation) |
117
|
|
|
{ |
118
|
|
|
return $this->instance->join($relation->getRelated()->getTable(), $relation->getQualifiedOtherKeyName(), '=', |
119
|
|
|
$relation->getQualifiedForeignKey()); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Join a has one relationship. |
124
|
|
|
* |
125
|
|
|
* @param HasOneOrMany $relation |
126
|
|
|
* |
127
|
|
|
* @return mixed |
128
|
|
|
*/ |
129
|
|
|
protected function joinHasOne(HasOneOrMany $relation) |
130
|
|
|
{ |
131
|
|
|
return $this->instance->join($relation->getRelated()->getTable(), |
132
|
|
|
$relation->getRelated()->getTable() . '.' . $relation->getParent()->getForeignKey(), '=', |
133
|
|
|
$relation->getParent()->getTable() . '.' . $relation->getParent()->getKeyName()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Append column sorting to query builder. |
138
|
|
|
* |
139
|
|
|
* @param string|array $column |
140
|
|
|
* @param string $direction |
141
|
|
|
* |
142
|
|
|
* @return $this |
143
|
|
|
*/ |
144
|
2 |
|
public function sortBy($column, $direction = 'ASC') |
145
|
|
|
{ |
146
|
2 |
|
$this->instance = $this->instance->orderBy($column, $direction); |
147
|
|
|
|
148
|
2 |
|
return $this; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
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: