1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ProtoneMedia\LaravelEloquentScopeAsSelect; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use Illuminate\Support\Facades\DB; |
8
|
|
|
|
9
|
|
|
class ScopeAsSelect |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Helper method for code completion. |
13
|
|
|
* |
14
|
|
|
* @param mixed $value |
15
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
16
|
|
|
*/ |
17
|
|
|
public static function builder($value): Builder |
18
|
|
|
{ |
19
|
|
|
return $value; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Returns a callable that applies the scope and arguments |
24
|
|
|
* to the given query builder. |
25
|
|
|
* |
26
|
|
|
* @param mixed $value |
27
|
|
|
* @return callable |
28
|
|
|
*/ |
29
|
|
|
public static function makeCallable($value): callable |
30
|
|
|
{ |
31
|
|
|
// We both allow single and multiple scopes... |
32
|
|
|
$scopes = Arr::wrap($value); |
33
|
|
|
|
34
|
|
|
return function ($query) use ($scopes) { |
35
|
|
|
// If $scope is numeric, there are no arguments, and we can |
36
|
|
|
// safely assume the scope is in the $arguments variable. |
37
|
|
|
foreach ($scopes as $scope => $arguments) { |
38
|
|
|
if (is_numeric($scope)) { |
39
|
|
|
[$scope, $arguments] = [$arguments, null]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
// As we allow a constraint to be a single arguments. |
43
|
|
|
$arguments = Arr::wrap($arguments); |
44
|
|
|
|
45
|
|
|
$query->{$scope}(...$arguments); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $query; |
49
|
|
|
}; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Adds a macro to the query builder. |
54
|
|
|
* |
55
|
|
|
* @param string $name |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
|
|
public static function addMacro(string $name = 'addScopeAsSelect') |
59
|
|
|
{ |
60
|
|
|
Builder::macro($name, function (string $name, $withQuery, bool $exists = true): Builder { |
61
|
|
|
$callable = is_callable($withQuery) |
62
|
|
|
? $withQuery |
63
|
|
|
: ScopeAsSelect::makeCallable($withQuery); |
64
|
|
|
|
65
|
|
|
// We do this to make sure the $query variable is an Eloquent Query Builder. |
66
|
|
|
$query = ScopeAsSelect::builder($this); |
67
|
|
|
|
68
|
|
|
$originalTable = $query->getModel()->getTable(); |
69
|
|
|
|
70
|
|
|
// Instantiate a new model that uses the aliased table. |
71
|
|
|
$aliasedTable = "{$name}_{$originalTable}"; |
72
|
|
|
$aliasedModel = $query->newModelInstance()->setTable($aliasedTable); |
73
|
|
|
|
74
|
|
|
// Query the model and explicitly set the targetted table, as the model's table |
75
|
|
|
// is just the aliased table with the 'as' statement. |
76
|
|
|
$subSelect = $aliasedModel::query()->setModel($aliasedModel); |
77
|
|
|
$subSelect->getQuery()->from($originalTable, $aliasedTable); |
78
|
|
|
|
79
|
|
|
// Apply the where constraint based on the model's key name and apply the $callable. |
80
|
|
|
$subSelect |
81
|
|
|
->select(DB::raw(1)) |
82
|
|
|
->whereColumn($aliasedModel->getQualifiedKeyName(), $query->getModel()->getQualifiedKeyName()) |
83
|
|
|
->limit(1) |
84
|
|
|
->tap(fn ($query) => $callable($query)); |
|
|
|
|
85
|
|
|
|
86
|
|
|
// Add the subquery and query-time cast. |
87
|
|
|
return $query |
88
|
|
|
->addSelect([$name => $subSelect]) |
89
|
|
|
->withCasts([$name => $exists ? NullableBooleanCaster::class : NegativeNullableBooleanCaster::class]); |
90
|
|
|
}); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|