|
1
|
|
|
<?php namespace App\Modules\Core\BaseClasses; |
|
2
|
|
|
|
|
3
|
|
|
use App\Modules\Core\Interfaces\BaseRepositoryInterface; |
|
4
|
|
|
use Illuminate\Support\Arr; |
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
|
|
7
|
|
|
abstract class BaseRepository implements BaseRepositoryInterface |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* The model implementation. |
|
11
|
|
|
* |
|
12
|
|
|
* @var object |
|
13
|
|
|
*/ |
|
14
|
|
|
public $model; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Init new object. |
|
18
|
|
|
* |
|
19
|
|
|
* @var mixed model |
|
20
|
|
|
* @return void |
|
|
|
|
|
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct($model) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->model = $model; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Fetch all records with relations from the storage. |
|
29
|
|
|
* |
|
30
|
|
|
* @param array $relations |
|
31
|
|
|
* @param string $sortBy |
|
32
|
|
|
* @param boolean $desc |
|
33
|
|
|
* @param array $columns |
|
34
|
|
|
* @return collection |
|
35
|
|
|
*/ |
|
36
|
|
|
public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
37
|
|
|
{ |
|
38
|
|
|
$sort = $desc ? 'desc' : 'asc'; |
|
39
|
|
|
return $this->model->with($relations)->orderBy($sortBy, $sort)->get($columns); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Fetch all records with relations from storage in pages |
|
44
|
|
|
* that matche the given query. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $query |
|
47
|
|
|
* @param integer $perPage |
|
48
|
|
|
* @param array $relations |
|
49
|
|
|
* @param string $sortBy |
|
50
|
|
|
* @param boolean $desc |
|
51
|
|
|
* @param array $columns |
|
52
|
|
|
* @return collection |
|
53
|
|
|
*/ |
|
54
|
|
|
public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
55
|
|
|
{ |
|
56
|
|
|
$model = $this->model->with($relations); |
|
57
|
|
|
$conditionColumns = $this->model->searchable; |
|
58
|
|
|
$sort = $desc ? 'desc' : 'asc'; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Construct the select conditions for the model. |
|
62
|
|
|
*/ |
|
63
|
|
|
$model->where(function ($q) use ($query, $conditionColumns, $relations) { |
|
64
|
|
|
|
|
65
|
|
View Code Duplication |
if (count($conditionColumns)) { |
|
|
|
|
|
|
66
|
|
|
$column = 'LOWER('.array_shift($conditionColumns).')'; |
|
67
|
|
|
if (Str::contains($column, '->')) { |
|
68
|
|
|
$column = $this->wrapJsonSelector($column); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Use the first element in the model columns to construct the first condition. |
|
73
|
|
|
*/ |
|
74
|
|
|
$q->where(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Loop through the rest of the columns to construct or where conditions. |
|
79
|
|
|
*/ |
|
80
|
|
View Code Duplication |
foreach ($conditionColumns as $column) { |
|
|
|
|
|
|
81
|
|
|
$column = 'LOWER('.$column.')'; |
|
82
|
|
|
if (Str::contains($column, '->')) { |
|
83
|
|
|
$column = $this->wrapJsonSelector($column); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$q->orWhere(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Loop through the model relations. |
|
91
|
|
|
*/ |
|
92
|
|
|
foreach ($relations as $relation) { |
|
93
|
|
|
/** |
|
94
|
|
|
* Remove the sub relation if exists. |
|
95
|
|
|
*/ |
|
96
|
|
|
$relation = explode('.', $relation)[0]; |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Try to fetch the relation repository from the core. |
|
100
|
|
|
*/ |
|
101
|
|
|
if (\Core::$relation()) { |
|
102
|
|
|
/** |
|
103
|
|
|
* Construct the relation condition. |
|
104
|
|
|
*/ |
|
105
|
|
|
$q->orWhereHas($relation, function ($subModel) use ($query, $relation) { |
|
106
|
|
|
|
|
107
|
|
|
$subModel->where(function ($q) use ($query, $relation) { |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Get columns of the relation. |
|
111
|
|
|
*/ |
|
112
|
|
|
$subConditionColumns = \Core::$relation()->model->searchable; |
|
113
|
|
|
|
|
114
|
|
View Code Duplication |
if (count($subConditionColumns)) { |
|
|
|
|
|
|
115
|
|
|
$column = 'LOWER('.array_shift($subConditionColumns).')'; |
|
116
|
|
|
if (Str::contains($column, '->')) { |
|
117
|
|
|
$column = $this->wrapJsonSelector($column); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Use the first element in the relation model columns to construct the first condition. |
|
122
|
|
|
*/ |
|
123
|
|
|
$q->where(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Loop through the rest of the columns to construct or where conditions. |
|
128
|
|
|
*/ |
|
129
|
|
View Code Duplication |
foreach ($subConditionColumns as $subConditionColumn) { |
|
|
|
|
|
|
130
|
|
|
$column = 'LOWER('.$subConditionColumn.')'; |
|
131
|
|
|
if (Str::contains($column, '->')) { |
|
132
|
|
|
$column = $this->wrapJsonSelector($column); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
$q->orWhere(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
136
|
|
|
} |
|
137
|
|
|
}); |
|
138
|
|
|
}); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
}); |
|
142
|
|
|
|
|
143
|
|
|
return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Fetch all records with relations from storage in pages. |
|
148
|
|
|
* |
|
149
|
|
|
* @param integer $perPage |
|
150
|
|
|
* @param array $relations |
|
151
|
|
|
* @param string $sortBy |
|
152
|
|
|
* @param boolean $desc |
|
153
|
|
|
* @param array $columns |
|
154
|
|
|
* @return collection |
|
155
|
|
|
*/ |
|
156
|
|
|
public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
157
|
|
|
{ |
|
158
|
|
|
$sort = $desc ? 'desc' : 'asc'; |
|
159
|
|
|
return $this->model->with($relations)->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Fetch all records with relations based on |
|
164
|
|
|
* the given condition from storage in pages. |
|
165
|
|
|
* |
|
166
|
|
|
* @param array $conditions array of conditions |
|
167
|
|
|
* @param integer $perPage |
|
168
|
|
|
* @param array $relations |
|
169
|
|
|
* @param string $sortBy |
|
170
|
|
|
* @param boolean $desc |
|
171
|
|
|
* @param array $columns |
|
172
|
|
|
* @return collection |
|
173
|
|
|
*/ |
|
174
|
|
|
public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
175
|
|
|
{ |
|
176
|
|
|
unset($conditions['page']); |
|
177
|
|
|
$conditions = $this->constructConditions($conditions, $this->model); |
|
178
|
|
|
$sort = $desc ? 'desc' : 'asc'; |
|
179
|
|
|
return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Save the given model to the storage. |
|
184
|
|
|
* |
|
185
|
|
|
* @param array $data |
|
186
|
|
|
* @return mixed |
|
187
|
|
|
*/ |
|
188
|
|
|
public function save(array $data) |
|
189
|
|
|
{ |
|
190
|
|
|
\Session::put('locale', 'all'); |
|
191
|
|
|
$model = false; |
|
192
|
|
|
$modelClass = $this->model; |
|
193
|
|
|
$relations = []; |
|
194
|
|
|
|
|
195
|
|
|
\DB::transaction(function () use (&$model, &$relations, $data, $modelClass) { |
|
196
|
|
|
/** |
|
197
|
|
|
* If the id is present in the data then select the model for updating, |
|
198
|
|
|
* else create new model. |
|
199
|
|
|
* @var array |
|
200
|
|
|
*/ |
|
201
|
|
|
$model = Arr::has($data, 'id') ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
202
|
|
|
if (! $model) { |
|
203
|
|
|
\ErrorHandler::notFound(class_basename($modelClass).' with id : '.$data['id']); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Construct the model object with the given data, |
|
208
|
|
|
* and if there is a relation add it to relations array, |
|
209
|
|
|
* then save the model. |
|
210
|
|
|
*/ |
|
211
|
|
|
foreach ($data as $key => $value) { |
|
212
|
|
|
/** |
|
213
|
|
|
* If the attribute is a relation. |
|
214
|
|
|
*/ |
|
215
|
|
|
$relation = camel_case($key); |
|
216
|
|
|
if (method_exists($model, $relation) && \Core::$relation()) { |
|
217
|
|
|
/** |
|
218
|
|
|
* Check if the relation is a collection. |
|
219
|
|
|
*/ |
|
220
|
|
|
if (class_basename($model->$relation) == 'Collection') { |
|
221
|
|
|
/** |
|
222
|
|
|
* If the relation has no value then marke the relation data |
|
223
|
|
|
* related to the model to be deleted. |
|
224
|
|
|
*/ |
|
225
|
|
|
if (! $value || ! count($value)) { |
|
226
|
|
|
$relations[$relation] = 'delete'; |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
if (is_array($value)) { |
|
230
|
|
|
/** |
|
231
|
|
|
* Loop through the relation data. |
|
232
|
|
|
*/ |
|
233
|
|
|
foreach ($value as $attr => $val) { |
|
234
|
|
|
/** |
|
235
|
|
|
* Get the relation model. |
|
236
|
|
|
*/ |
|
237
|
|
|
$relationBaseModel = \Core::$relation()->model; |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Check if the relation is a collection. |
|
241
|
|
|
*/ |
|
242
|
|
|
if (class_basename($model->$relation) == 'Collection') { |
|
243
|
|
|
/** |
|
244
|
|
|
* If the id is present in the data then select the relation model for updating, |
|
245
|
|
|
* else create new model. |
|
246
|
|
|
*/ |
|
247
|
|
|
$relationModel = Arr::has($val, 'id') ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* If model doesn't exists. |
|
251
|
|
|
*/ |
|
252
|
|
|
if (! $relationModel) { |
|
253
|
|
|
\ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Loop through the relation attributes. |
|
258
|
|
|
*/ |
|
259
|
|
|
foreach ($val as $attr => $val) { |
|
260
|
|
|
/** |
|
261
|
|
|
* Prevent the sub relations or attributes not in the fillable. |
|
262
|
|
|
*/ |
|
263
|
|
|
if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) { |
|
264
|
|
|
$relationModel->$attr = $val; |
|
265
|
|
|
} |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
$relations[$relation][] = $relationModel; |
|
269
|
|
|
} else { |
|
270
|
|
|
/** |
|
271
|
|
|
* Prevent the sub relations. |
|
272
|
|
|
*/ |
|
273
|
|
|
if (gettype($val) !== 'object' && gettype($val) !== 'array') { |
|
274
|
|
|
/** |
|
275
|
|
|
* If the id is present in the data then select the relation model for updating, |
|
276
|
|
|
* else create new model. |
|
277
|
|
|
*/ |
|
278
|
|
|
$relationModel = Arr::has($value, 'id') ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* If model doesn't exists. |
|
282
|
|
|
*/ |
|
283
|
|
|
if (! $relationModel) { |
|
284
|
|
|
\ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
foreach ($value as $relationAttribute => $relationValue) { |
|
288
|
|
|
/** |
|
289
|
|
|
* Prevent attributes not in the fillable. |
|
290
|
|
|
*/ |
|
291
|
|
|
if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) { |
|
292
|
|
|
$relationModel->$relationAttribute = $relationValue; |
|
293
|
|
|
} |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
$relations[$relation] = $relationModel; |
|
297
|
|
|
} |
|
298
|
|
|
} |
|
299
|
|
|
} |
|
300
|
|
|
} |
|
301
|
|
|
} elseif (array_search($key, $model->getFillable(), true) !== false) { |
|
302
|
|
|
/** |
|
303
|
|
|
* If the attribute isn't a relation and prevent attributes not in the fillable. |
|
304
|
|
|
*/ |
|
305
|
|
|
$model->$key = $value; |
|
306
|
|
|
} |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
/** |
|
310
|
|
|
* Loop through the relations array. |
|
311
|
|
|
*/ |
|
312
|
|
|
foreach ($relations as $key => $value) { |
|
313
|
|
|
/** |
|
314
|
|
|
* If the relation is marked for delete then delete it. |
|
315
|
|
|
*/ |
|
316
|
|
|
if ($value == 'delete' && $model->$key()->count()) { |
|
317
|
|
|
$model->$key()->delete(); |
|
318
|
|
|
} elseif (gettype($value) == 'array') { |
|
319
|
|
|
/** |
|
320
|
|
|
* Save the model. |
|
321
|
|
|
*/ |
|
322
|
|
|
$model->save(); |
|
323
|
|
|
$ids = []; |
|
324
|
|
|
|
|
325
|
|
|
/** |
|
326
|
|
|
* Loop through the relations. |
|
327
|
|
|
*/ |
|
328
|
|
|
foreach ($value as $val) { |
|
329
|
|
|
switch (class_basename($model->$key())) { |
|
330
|
|
|
/** |
|
331
|
|
|
* If the relation is one to many then update it's foreign key with |
|
332
|
|
|
* the model id and save it then add its id to ids array to delete all |
|
333
|
|
|
* relations who's id isn't in the ids array. |
|
334
|
|
|
*/ |
|
335
|
|
|
case 'HasMany': |
|
336
|
|
|
$foreignKeyName = $model->$key()->getForeignKeyName(); |
|
337
|
|
|
$val->$foreignKeyName = $model->id; |
|
338
|
|
|
$val->save(); |
|
339
|
|
|
$ids[] = $val->id; |
|
340
|
|
|
break; |
|
341
|
|
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* If the relation is many to many then add it's id to the ids array to |
|
344
|
|
|
* attache these ids to the model. |
|
345
|
|
|
*/ |
|
346
|
|
|
case 'BelongsToMany': |
|
347
|
|
|
$val->save(); |
|
348
|
|
|
$ids[] = $val->id; |
|
349
|
|
|
break; |
|
350
|
|
|
} |
|
351
|
|
|
} |
|
352
|
|
|
switch (class_basename($model->$key())) { |
|
353
|
|
|
/** |
|
354
|
|
|
* If the relation is one to many then delete all |
|
355
|
|
|
* relations who's id isn't in the ids array. |
|
356
|
|
|
*/ |
|
357
|
|
|
case 'HasMany': |
|
358
|
|
|
$model->$key()->whereNotIn('id', $ids)->delete(); |
|
359
|
|
|
break; |
|
360
|
|
|
|
|
361
|
|
|
/** |
|
362
|
|
|
* If the relation is many to many then |
|
363
|
|
|
* detach the previous data and attach |
|
364
|
|
|
* the ids array to the model. |
|
365
|
|
|
*/ |
|
366
|
|
|
case 'BelongsToMany': |
|
367
|
|
|
$model->$key()->detach(); |
|
368
|
|
|
$model->$key()->attach($ids); |
|
369
|
|
|
break; |
|
370
|
|
|
} |
|
371
|
|
|
} else { |
|
372
|
|
|
switch (class_basename($model->$key())) { |
|
373
|
|
|
/** |
|
374
|
|
|
* If the relation is one to one. |
|
375
|
|
|
*/ |
|
376
|
|
|
case 'HasOne': |
|
377
|
|
|
/** |
|
378
|
|
|
* Save the model. |
|
379
|
|
|
*/ |
|
380
|
|
|
$model->save(); |
|
381
|
|
|
$foreignKeyName = $model->$key()->getForeignKeyName(); |
|
382
|
|
|
$value->$foreignKeyName = $model->id; |
|
383
|
|
|
$value->save(); |
|
384
|
|
|
break; |
|
385
|
|
|
case 'BelongsTo': |
|
386
|
|
|
/** |
|
387
|
|
|
* Save the model. |
|
388
|
|
|
*/ |
|
389
|
|
|
$value->save(); |
|
390
|
|
|
$model->$key()->associate($value); |
|
391
|
|
|
break; |
|
392
|
|
|
} |
|
393
|
|
|
} |
|
394
|
|
|
} |
|
395
|
|
|
|
|
396
|
|
|
/** |
|
397
|
|
|
* Save the model. |
|
398
|
|
|
*/ |
|
399
|
|
|
$model->save(); |
|
400
|
|
|
}); |
|
401
|
|
|
|
|
402
|
|
|
return $model; |
|
403
|
|
|
} |
|
404
|
|
|
|
|
405
|
|
|
/** |
|
406
|
|
|
* Update record in the storage based on the given |
|
407
|
|
|
* condition. |
|
408
|
|
|
* |
|
409
|
|
|
* @param var $value condition value |
|
410
|
|
|
* @param array $data |
|
411
|
|
|
* @param string $attribute condition column name |
|
412
|
|
|
* @return void |
|
413
|
|
|
*/ |
|
414
|
|
|
public function update($value, array $data, $attribute = 'id') |
|
415
|
|
|
{ |
|
416
|
|
|
if ($attribute == 'id') { |
|
417
|
|
|
$model = $this->model->lockForUpdate()->find($value); |
|
418
|
|
|
$model ? $model->update($data) : 0; |
|
419
|
|
|
} else { |
|
420
|
|
|
$this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function ($model) use ($data) { |
|
421
|
|
|
$model->update($data); |
|
422
|
|
|
}); |
|
423
|
|
|
} |
|
424
|
|
|
} |
|
425
|
|
|
|
|
426
|
|
|
/** |
|
427
|
|
|
* Delete record from the storage based on the given |
|
428
|
|
|
* condition. |
|
429
|
|
|
* |
|
430
|
|
|
* @param var $value condition value |
|
431
|
|
|
* @param string $attribute condition column name |
|
432
|
|
|
* @return void |
|
433
|
|
|
*/ |
|
434
|
|
|
public function delete($value, $attribute = 'id') |
|
435
|
|
|
{ |
|
436
|
|
|
if ($attribute == 'id') { |
|
437
|
|
View Code Duplication |
\DB::transaction(function () use ($value) { |
|
|
|
|
|
|
438
|
|
|
$model = $this->model->lockForUpdate()->find($value); |
|
439
|
|
|
if (! $model) { |
|
440
|
|
|
\ErrorHandler::notFound(class_basename($this->model).' with id : '.$value); |
|
441
|
|
|
} |
|
442
|
|
|
|
|
443
|
|
|
$model->delete(); |
|
444
|
|
|
}); |
|
445
|
|
|
} else { |
|
446
|
|
|
\DB::transaction(function () use ($value, $attribute) { |
|
447
|
|
|
$this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function ($model) { |
|
448
|
|
|
$model->delete(); |
|
449
|
|
|
}); |
|
450
|
|
|
}); |
|
451
|
|
|
} |
|
452
|
|
|
} |
|
453
|
|
|
|
|
454
|
|
|
/** |
|
455
|
|
|
* Fetch records from the storage based on the given |
|
456
|
|
|
* id. |
|
457
|
|
|
* |
|
458
|
|
|
* @param integer $id |
|
459
|
|
|
* @param string[] $relations |
|
460
|
|
|
* @param array $columns |
|
461
|
|
|
* @return object |
|
462
|
|
|
*/ |
|
463
|
|
|
public function find($id, $relations = [], $columns = ['*']) |
|
464
|
|
|
{ |
|
465
|
|
|
return $this->model->with($relations)->find($id, $columns); |
|
466
|
|
|
} |
|
467
|
|
|
|
|
468
|
|
|
/** |
|
469
|
|
|
* Fetch records from the storage based on the given |
|
470
|
|
|
* condition. |
|
471
|
|
|
* |
|
472
|
|
|
* @param array $conditions array of conditions |
|
473
|
|
|
* @param array $relations |
|
474
|
|
|
* @param string $sortBy |
|
475
|
|
|
* @param boolean $desc |
|
476
|
|
|
* @param array $columns |
|
477
|
|
|
* @return collection |
|
478
|
|
|
*/ |
|
479
|
|
|
public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
480
|
|
|
{ |
|
481
|
|
|
$conditions = $this->constructConditions($conditions, $this->model); |
|
482
|
|
|
$sort = $desc ? 'desc' : 'asc'; |
|
483
|
|
|
return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
484
|
|
|
} |
|
485
|
|
|
|
|
486
|
|
|
/** |
|
487
|
|
|
* Fetch the first record from the storage based on the given |
|
488
|
|
|
* condition. |
|
489
|
|
|
* |
|
490
|
|
|
* @param array $conditions array of conditions |
|
491
|
|
|
* @param array $relations |
|
492
|
|
|
* @param array $columns |
|
493
|
|
|
* @return object |
|
494
|
|
|
*/ |
|
495
|
|
|
public function first($conditions, $relations = [], $columns = ['*']) |
|
496
|
|
|
{ |
|
497
|
|
|
$conditions = $this->constructConditions($conditions, $this->model); |
|
498
|
|
|
return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
499
|
|
|
} |
|
500
|
|
|
|
|
501
|
|
|
/** |
|
502
|
|
|
* Return the deleted models in pages based on the given conditions. |
|
503
|
|
|
* |
|
504
|
|
|
* @param array $conditions array of conditions |
|
505
|
|
|
* @param integer $perPage |
|
506
|
|
|
* @param string $sortBy |
|
507
|
|
|
* @param boolean $desc |
|
508
|
|
|
* @param array $columns |
|
509
|
|
|
* @return collection |
|
510
|
|
|
*/ |
|
511
|
|
|
public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
512
|
|
|
{ |
|
513
|
|
|
unset($conditions['page']); |
|
514
|
|
|
$conditions = $this->constructConditions($conditions, $this->model); |
|
515
|
|
|
$sort = $desc ? 'desc' : 'asc'; |
|
516
|
|
|
$model = $this->model->onlyTrashed(); |
|
517
|
|
|
|
|
518
|
|
|
if (count($conditions['conditionValues'])) { |
|
519
|
|
|
$model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
520
|
|
|
} |
|
521
|
|
|
|
|
522
|
|
|
return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
523
|
|
|
} |
|
524
|
|
|
|
|
525
|
|
|
/** |
|
526
|
|
|
* Restore the deleted model. |
|
527
|
|
|
* |
|
528
|
|
|
* @param integer $id |
|
529
|
|
|
* @return void |
|
530
|
|
|
*/ |
|
531
|
|
View Code Duplication |
public function restore($id) |
|
|
|
|
|
|
532
|
|
|
{ |
|
533
|
|
|
$model = $this->model->onlyTrashed()->find($id); |
|
534
|
|
|
|
|
535
|
|
|
if (! $model) { |
|
536
|
|
|
\ErrorHandler::notFound(class_basename($this->model).' with id : '.$id); |
|
537
|
|
|
} |
|
538
|
|
|
|
|
539
|
|
|
$model->restore(); |
|
540
|
|
|
} |
|
541
|
|
|
|
|
542
|
|
|
/** |
|
543
|
|
|
* Build the conditions recursively for the retrieving methods. |
|
544
|
|
|
* @param array $conditions |
|
545
|
|
|
* @return array |
|
546
|
|
|
*/ |
|
547
|
|
|
protected function constructConditions($conditions, $model) |
|
548
|
|
|
{ |
|
549
|
|
|
$conditionString = ''; |
|
550
|
|
|
$conditionValues = []; |
|
551
|
|
|
foreach ($conditions as $key => $value) { |
|
552
|
|
|
if (Str::contains($key, '->')) { |
|
553
|
|
|
$key = $this->wrapJsonSelector($key); |
|
554
|
|
|
} |
|
555
|
|
|
|
|
556
|
|
|
if ($key == 'and') { |
|
557
|
|
|
$conditions = $this->constructConditions($value, $model); |
|
558
|
|
|
$conditionString .= str_replace('{op}', 'and', $conditions['conditionString']).' {op} '; |
|
559
|
|
|
$conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
560
|
|
|
} elseif ($key == 'or') { |
|
561
|
|
|
$conditions = $this->constructConditions($value, $model); |
|
562
|
|
|
$conditionString .= str_replace('{op}', 'or', $conditions['conditionString']).' {op} '; |
|
563
|
|
|
$conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
564
|
|
|
} else { |
|
565
|
|
|
if (is_array($value)) { |
|
566
|
|
|
$operator = $value['op']; |
|
567
|
|
|
if (strtolower($operator) == 'between') { |
|
568
|
|
|
$value1 = $value['val1']; |
|
569
|
|
|
$value2 = $value['val2']; |
|
570
|
|
|
} else { |
|
571
|
|
|
$value = Arr::get($value, 'val', ''); |
|
572
|
|
|
} |
|
573
|
|
|
} else { |
|
574
|
|
|
$operator = '='; |
|
575
|
|
|
} |
|
576
|
|
|
|
|
577
|
|
|
if (strtolower($operator) == 'between') { |
|
578
|
|
|
$conditionString .= $key.' >= ? and '; |
|
579
|
|
|
$conditionValues[] = $value1; |
|
|
|
|
|
|
580
|
|
|
|
|
581
|
|
|
$conditionString .= $key.' <= ? {op} '; |
|
582
|
|
|
$conditionValues[] = $value2; |
|
|
|
|
|
|
583
|
|
|
} elseif (strtolower($operator) == 'in') { |
|
584
|
|
|
$conditionValues = array_merge($conditionValues, $value); |
|
585
|
|
|
$inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
586
|
|
|
$conditionString .= $key.' in ('.rtrim($inBindingsString, ',').') {op} '; |
|
587
|
|
|
} elseif (strtolower($operator) == 'null') { |
|
588
|
|
|
$conditionString .= $key.' is null {op} '; |
|
589
|
|
|
} elseif (strtolower($operator) == 'not null') { |
|
590
|
|
|
$conditionString .= $key.' is not null {op} '; |
|
591
|
|
|
} elseif (strtolower($operator) == 'has') { |
|
592
|
|
|
$sql = $model->withTrashed()->has($key)->toSql(); |
|
593
|
|
|
$conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
594
|
|
|
$conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')').' and '.$conditions['conditionString'].') {op} '; |
|
595
|
|
|
$conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
596
|
|
|
} else { |
|
597
|
|
|
$conditionString .= $key.' '.$operator.' ? {op} '; |
|
598
|
|
|
$conditionValues[] = $value; |
|
599
|
|
|
} |
|
600
|
|
|
} |
|
601
|
|
|
} |
|
602
|
|
|
$conditionString = '('.rtrim($conditionString, '{op} ').')'; |
|
603
|
|
|
return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
604
|
|
|
} |
|
605
|
|
|
|
|
606
|
|
|
/** |
|
607
|
|
|
* Wrap the given JSON selector. |
|
608
|
|
|
* |
|
609
|
|
|
* @param string $value |
|
610
|
|
|
* @return string |
|
611
|
|
|
*/ |
|
612
|
|
|
protected function wrapJsonSelector($value) |
|
613
|
|
|
{ |
|
614
|
|
|
$removeLast = strpos($value, ')'); |
|
615
|
|
|
$value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
616
|
|
|
$path = explode('->', $value); |
|
617
|
|
|
$field = array_shift($path); |
|
618
|
|
|
$result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
619
|
|
|
return '"'.$part.'"'; |
|
620
|
|
|
})->implode('.')); |
|
621
|
|
|
|
|
622
|
|
|
return $removeLast === false ? $result : $result.')'; |
|
623
|
|
|
} |
|
624
|
|
|
} |
|
625
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.