Completed
Push — develop ( b3c629...8e0062 )
by Abdelrahman
04:19
created

EloquentBuilder::pluck()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 34
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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