1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\QueryBuilder; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Spatie\QueryBuilder\Concerns\AddsFieldsToQuery; |
8
|
|
|
use Spatie\QueryBuilder\Concerns\AddsIncludesToQuery; |
9
|
|
|
use Spatie\QueryBuilder\Concerns\AppendsAttributesToResults; |
10
|
|
|
use Spatie\QueryBuilder\Concerns\FiltersQuery; |
11
|
|
|
use Spatie\QueryBuilder\Concerns\SortsQuery; |
12
|
|
|
|
13
|
|
|
class QueryBuilder extends Builder |
14
|
|
|
{ |
15
|
|
|
use FiltersQuery, |
16
|
|
|
SortsQuery, |
17
|
|
|
AddsIncludesToQuery, |
18
|
|
|
AddsFieldsToQuery, |
19
|
|
|
AppendsAttributesToResults; |
20
|
|
|
|
21
|
|
|
/** @var \Spatie\QueryBuilder\QueryBuilderRequest */ |
22
|
|
|
protected $request; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* QueryBuilder constructor. |
26
|
|
|
* |
27
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation $builder |
28
|
|
|
* @param null|\Illuminate\Http\Request $request |
29
|
|
|
*/ |
30
|
|
|
public function __construct($builder, ?Request $request = null) |
31
|
|
|
{ |
32
|
|
|
parent::__construct(clone $builder->getQuery()); |
33
|
|
|
|
34
|
|
|
$this->initializeFromBuilder($builder); |
35
|
|
|
|
36
|
|
|
$this->request = $request |
37
|
|
|
? QueryBuilderRequest::fromRequest($request) |
38
|
|
|
: app(QueryBuilderRequest::class); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Create a new QueryBuilder for a request and model. |
43
|
|
|
* |
44
|
|
|
* @param string|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation $baseQuery Model class or base query builder |
45
|
|
|
* @param \Illuminate\Http\Request $request |
46
|
|
|
* |
47
|
|
|
* @return \Spatie\QueryBuilder\QueryBuilder |
48
|
|
|
*/ |
49
|
|
|
public static function for($baseQuery, ?Request $request = null): self |
50
|
|
|
{ |
51
|
|
|
if (is_string($baseQuery)) { |
52
|
|
|
/** @var Builder $baseQuery */ |
53
|
|
|
$baseQuery = $baseQuery::query(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return new static($baseQuery, $request ?? app(Request::class)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function get($columns = ['*']) |
63
|
|
|
{ |
64
|
|
|
$results = parent::get($columns); |
65
|
|
|
|
66
|
|
|
if ($this->request->appends()->isNotEmpty()) { |
67
|
|
|
$results = $this->addAppendsToResults($results); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $results; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Add the model, scopes, eager loaded relationships, local macro's and onDelete callback |
75
|
|
|
* from the $builder to this query builder. |
76
|
|
|
* |
77
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
78
|
|
|
*/ |
79
|
|
|
protected function initializeFromBuilder(Builder $builder) |
80
|
|
|
{ |
81
|
|
|
$this |
82
|
|
|
->setModel($builder->getModel()) |
|
|
|
|
83
|
|
|
->setEagerLoads($builder->getEagerLoads()); |
84
|
|
|
|
85
|
|
|
$builder->macro('getProtected', function (Builder $builder, string $property) { |
86
|
|
|
return $builder->{$property}; |
87
|
|
|
}); |
88
|
|
|
|
89
|
|
|
$this->scopes = $builder->getProtected('scopes'); |
90
|
|
|
|
91
|
|
|
$this->localMacros = $builder->getProtected('localMacros'); |
92
|
|
|
|
93
|
|
|
$this->onDelete = $builder->getProtected('onDelete'); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.