1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Volosyuk\SimpleEloquent; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
7
|
|
|
use Illuminate\Pagination\LengthAwarePaginator; |
8
|
|
|
use Illuminate\Pagination\Paginator; |
9
|
|
|
use Illuminate\Support\Collection; |
10
|
|
|
use stdClass; |
11
|
|
|
use Volosyuk\SimpleEloquent\Relations\Relation; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Builder |
15
|
|
|
* @package Volosyuk\SimpleEloquent |
16
|
|
|
*/ |
17
|
|
|
class Builder extends \Illuminate\Database\Eloquent\Builder |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @param array $columns |
21
|
|
|
* @return Collection |
22
|
|
|
*/ |
23
|
19 |
|
public function getSimple($columns = ['*']) |
24
|
|
|
{ |
25
|
19 |
|
$builder = $this->applyScopes(); |
26
|
|
|
|
27
|
19 |
|
$models = $builder->getSimpleModels($columns); |
28
|
|
|
|
29
|
19 |
|
if (count($models) > 0) { |
|
|
|
|
30
|
19 |
|
$models = $builder->eagerLoadRelationsSimple($models); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
19 |
|
return collect($models); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param mixed $id |
38
|
|
|
* @param array $columns |
39
|
|
|
* @return Collection|stdClass|array|null |
40
|
|
|
*/ |
41
|
2 |
|
public function findSimple($id, $columns = ['*']) |
42
|
|
|
{ |
43
|
2 |
|
if (is_array($id)) { |
44
|
2 |
|
return $this->findManySimple($id, $columns); |
45
|
|
|
} |
46
|
|
|
|
47
|
2 |
|
$this->query->where($this->model->getQualifiedKeyName(), '=', $id); |
48
|
|
|
|
49
|
2 |
|
return $this->firstSimple($columns); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Find a model by its primary key or throw an exception. |
54
|
|
|
* |
55
|
|
|
* @param mixed $id |
56
|
|
|
* @param array $columns |
57
|
|
|
* @return stdClass|array|Collection |
58
|
|
|
* |
59
|
|
|
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
60
|
|
|
*/ |
61
|
1 |
|
public function findSimpleOrFail($id, $columns = ['*']) |
62
|
|
|
{ |
63
|
1 |
|
$result = $this->findSimple($id, $columns); |
64
|
|
|
|
65
|
1 |
|
if (is_array($id)) { |
66
|
1 |
|
if (count($result) == count(array_unique($id))) { |
|
|
|
|
67
|
1 |
|
return $result; |
68
|
|
|
} |
69
|
1 |
|
} elseif (! is_null($result)) { |
70
|
|
|
return $result; |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
throw (new ModelNotFoundException)->setModel( |
74
|
1 |
|
get_class($this->model), $id |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Execute the query and get the first result. |
80
|
|
|
* |
81
|
|
|
* @param array $columns |
82
|
|
|
* @return stdClass|array|null |
83
|
|
|
*/ |
84
|
14 |
|
public function firstSimple($columns = ['*']) |
85
|
|
|
{ |
86
|
14 |
|
return $this->take(1)->getSimple($columns)->first(); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Execute the query and get the first result or throw an exception. |
91
|
|
|
* |
92
|
|
|
* @param array $columns |
93
|
|
|
* @return array|stdClass |
94
|
|
|
* |
95
|
|
|
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
96
|
|
|
*/ |
97
|
1 |
|
public function firstSimpleOrFail($columns = ['*']) |
98
|
|
|
{ |
99
|
1 |
|
if (! is_null($model = $this->firstSimple($columns))) { |
100
|
1 |
|
return $model; |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
throw (new ModelNotFoundException)->setModel(get_class($this->model)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Find multiple models by their primary keys. |
108
|
|
|
* |
109
|
|
|
* @param array $ids |
110
|
|
|
* @param array $columns |
111
|
|
|
* @return Collection |
112
|
|
|
*/ |
113
|
3 |
|
public function findManySimple($ids, $columns = ['*']) |
114
|
|
|
{ |
115
|
3 |
|
if (empty($ids)) { |
116
|
1 |
|
return Collection::make([]); |
117
|
|
|
} |
118
|
|
|
|
119
|
3 |
|
$this->query->whereIn($this->model->getQualifiedKeyName(), $ids); |
120
|
|
|
|
121
|
3 |
|
return $this->getSimple($columns); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Paginate the given query. |
126
|
|
|
* |
127
|
|
|
* @param int $perPage |
128
|
|
|
* @param array $columns |
129
|
|
|
* @param string $pageName |
130
|
|
|
* @param int|null $page |
131
|
|
|
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator |
132
|
|
|
* |
133
|
|
|
* @throws \InvalidArgumentException |
134
|
|
|
*/ |
135
|
1 |
|
public function paginateSimple($perPage = null, $columns = ['*'], $pageName = 'page', $page = null) |
136
|
|
|
{ |
137
|
1 |
|
$page = $page ?: Paginator::resolveCurrentPage($pageName); |
138
|
|
|
|
139
|
1 |
|
$perPage = $perPage ?: $this->model->getPerPage(); |
140
|
|
|
|
141
|
1 |
|
$query = $this->toBase(); |
142
|
|
|
|
143
|
1 |
|
$total = $query->getCountForPagination(); |
144
|
|
|
|
145
|
1 |
|
$results = $total |
146
|
1 |
|
? $this->forPage($page, $perPage)->getSimple($columns) |
|
|
|
|
147
|
1 |
|
: $this->model->newCollection(); |
148
|
|
|
|
149
|
1 |
|
return new LengthAwarePaginator($results, $total, $perPage, $page, [ |
150
|
1 |
|
'path' => Paginator::resolveCurrentPath(), |
151
|
1 |
|
'pageName' => $pageName, |
152
|
|
|
]); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Get a paginator only supporting simple next and previous links. |
157
|
|
|
* |
158
|
|
|
* This is more efficient on larger data-sets, etc. |
159
|
|
|
* |
160
|
|
|
* @param int $perPage |
161
|
|
|
* @param array $columns |
162
|
|
|
* @param string $pageName |
163
|
|
|
* @param int|null $page |
164
|
|
|
* @return \Illuminate\Contracts\Pagination\Paginator |
165
|
|
|
*/ |
166
|
1 |
|
public function simplePaginateSimple($perPage = 15, $columns = ['*'], $pageName = 'page', $page = null) |
167
|
|
|
{ |
168
|
1 |
|
$page = $page ?: Paginator::resolveCurrentPage($pageName); |
169
|
|
|
|
170
|
1 |
|
$this->skip(($page - 1) * $perPage)->take($perPage + 1); |
|
|
|
|
171
|
|
|
|
172
|
1 |
|
return $this->simplePaginator($this->getSimple($columns), $perPage, $page, [ |
173
|
1 |
|
'path' => Paginator::resolveCurrentPath(), |
174
|
1 |
|
'pageName' => $pageName, |
175
|
|
|
]); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Get simple models without eager loading. |
180
|
|
|
* |
181
|
|
|
* @param array $columns |
182
|
|
|
* @return stdClass[]|array |
183
|
|
|
*/ |
184
|
19 |
|
public function getSimpleModels($columns = ['*']) |
185
|
|
|
{ |
186
|
19 |
|
return $this->query->get($columns)->all(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Eagerly load the relationship on a set of models. |
191
|
|
|
* |
192
|
|
|
* @param array $models |
193
|
|
|
* @param string $name |
194
|
|
|
* @param \Closure $constraints |
195
|
|
|
* @return array |
196
|
|
|
*/ |
197
|
10 |
|
protected function loadSimpleRelation(array $models, $name, Closure $constraints) |
198
|
|
|
{ |
199
|
10 |
|
$relation = $this->getRelation($name); |
200
|
|
|
|
201
|
10 |
|
$relation->addEagerConstraintsSimple($models); |
202
|
|
|
|
203
|
10 |
|
$constraints($relation); |
204
|
|
|
|
205
|
10 |
|
$models = $relation->initSimpleRelation($models, $name); |
206
|
|
|
|
207
|
10 |
|
return $relation->eagerLoadAndMatchSimple($models, $name); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Eager load the relationships for the models. |
212
|
|
|
* |
213
|
|
|
* @param array $models |
214
|
|
|
* @return array |
215
|
|
|
*/ |
216
|
19 |
|
public function eagerLoadRelationsSimple(array $models) |
217
|
|
|
{ |
218
|
19 |
|
foreach ($this->eagerLoad as $name => $constraints) { |
219
|
10 |
|
if (strpos($name, '.') === false) { |
220
|
10 |
|
$models = $this->loadSimpleRelation($models, $name, $constraints); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
19 |
|
return $models; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|