|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Transmissor\Cacheable; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Support\Collection; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @mixin \Illuminate\Database\Query\Builder |
|
12
|
|
|
*/ |
|
13
|
|
|
class EloquentBuilder extends Builder |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Execute the query as a "select" statement. |
|
17
|
|
|
* |
|
18
|
|
|
* @param array $columns |
|
19
|
|
|
* |
|
20
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|static[] |
|
21
|
|
|
*/ |
|
22
|
|
|
public function get($columns = ['*']) |
|
23
|
|
|
{ |
|
24
|
|
|
$builder = $this->applyScopes(); |
|
25
|
|
|
|
|
26
|
|
|
$closure = function () use ($builder, $columns) { |
|
27
|
|
|
// If we actually found models we will also eager load any relationships that |
|
28
|
|
|
// have been specified as needing to be eager loaded, which will solve the |
|
29
|
|
|
// n+1 query issue for the developers to avoid running a lot of queries. |
|
30
|
|
|
if (count($models = $builder->getModels($columns)) > 0) { |
|
31
|
|
|
$models = $builder->eagerLoadRelations($models); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
return $builder->model->newCollection($models); |
|
35
|
|
|
}; |
|
36
|
|
|
|
|
37
|
|
|
// Check if cache is enabled |
|
38
|
|
|
if ($builder->model->getCacheLifetime()) { |
|
39
|
|
|
return $builder->model->cacheQuery($builder, $columns, $closure); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
// Cache disabled, just execute query & return result |
|
43
|
|
|
$results = call_user_func($closure); |
|
44
|
|
|
|
|
45
|
|
|
// We're done, let's clean up! |
|
46
|
|
|
$builder->model->resetCacheConfig(); |
|
47
|
|
|
|
|
48
|
|
|
return $results; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get an array with the values of a given column. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $column |
|
55
|
|
|
* @param string|null $key |
|
56
|
|
|
* |
|
57
|
|
|
* @return \Illuminate\Support\Collection |
|
58
|
|
|
*/ |
|
59
|
|
|
public function pluck($column, $key = null): Collection |
|
60
|
|
|
{ |
|
61
|
|
|
$builder = $this->toBase(); |
|
62
|
|
|
|
|
63
|
|
|
$closure = function () use ($builder, $column, $key) { |
|
64
|
|
|
$results = $builder->pluck($column, $key); |
|
65
|
|
|
|
|
66
|
|
|
// If the model has a mutator for the requested column, we will spin through |
|
67
|
|
|
// the results and mutate the values so that the mutated version of these |
|
68
|
|
|
// columns are returned as you would expect from these Eloquent models. |
|
69
|
|
|
if (! $this->model->hasGetMutator($column) |
|
70
|
|
|
&& ! $this->model->hasCast($column) |
|
71
|
|
|
&& ! in_array($column, $this->model->getDates()) |
|
72
|
|
|
) { |
|
73
|
|
|
return $results; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $results->map( |
|
77
|
|
|
function ($value) use ($column) { |
|
78
|
|
|
return $this->model->newFromBuilder([$column => $value])->{$column}; |
|
79
|
|
|
} |
|
80
|
|
|
); |
|
81
|
|
|
}; |
|
82
|
|
|
|
|
83
|
|
|
// Check if cache is enabled |
|
84
|
|
|
if ($this->model->getCacheLifetime()) { |
|
85
|
|
|
return $this->model->cacheQuery($builder, (array) $column, $closure); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
// Cache disabled, just execute query & return result |
|
89
|
|
|
$results = call_user_func($closure); |
|
90
|
|
|
|
|
91
|
|
|
// We're done, let's clean up! |
|
92
|
|
|
$this->model->resetCacheConfig(); |
|
93
|
|
|
|
|
94
|
|
|
return $results; |
|
95
|
|
|
} |
|
96
|
|
|
} |