|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace ProtoneMedia\LaravelEloquentWhereNot; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
6
|
|
|
use Illuminate\Support\Arr; |
|
7
|
|
|
use Illuminate\Support\Facades\DB; |
|
8
|
|
|
|
|
9
|
|
|
class WhereNot |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* The count for each table. |
|
13
|
|
|
* |
|
14
|
|
|
* @var array |
|
15
|
|
|
*/ |
|
16
|
|
|
protected static $tableSubCount = []; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Makes an alias for the given table. |
|
20
|
|
|
* |
|
21
|
|
|
* @param string $table |
|
22
|
|
|
* @return string |
|
23
|
|
|
*/ |
|
24
|
|
|
public static function getTableAlias($table): string |
|
25
|
|
|
{ |
|
26
|
|
|
if (!array_key_exists($table, static::$tableSubCount)) { |
|
27
|
|
|
static::$tableSubCount[$table] = 0; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
$count = static::$tableSubCount[$table]++; |
|
31
|
|
|
|
|
32
|
|
|
return "where_not_{$count}_{$table}"; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Helper method for code completion. |
|
37
|
|
|
* |
|
38
|
|
|
* @param mixed $value |
|
39
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
40
|
|
|
*/ |
|
41
|
|
|
public static function builder($value): Builder |
|
42
|
|
|
{ |
|
43
|
|
|
return $value; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Returns a callable that applies the scope and arguments |
|
48
|
|
|
* to the given query builder. |
|
49
|
|
|
* |
|
50
|
|
|
* @param mixed $value |
|
51
|
|
|
* @return callable |
|
52
|
|
|
*/ |
|
53
|
|
|
public static function makeCallable($value): callable |
|
54
|
|
|
{ |
|
55
|
|
|
// We both allow single and multiple scopes... |
|
56
|
|
|
$scopes = Arr::wrap($value); |
|
57
|
|
|
|
|
58
|
|
|
return function ($query) use ($scopes) { |
|
59
|
|
|
// If $scope is numeric, there are no arguments, and we can |
|
60
|
|
|
// safely assume the scope is in the $arguments variable. |
|
61
|
|
|
foreach ($scopes as $scope => $arguments) { |
|
62
|
|
|
if (is_numeric($scope)) { |
|
63
|
|
|
[$scope, $arguments] = [$arguments, null]; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// As we allow a constraint to be a single arguments. |
|
67
|
|
|
$arguments = Arr::wrap($arguments); |
|
68
|
|
|
|
|
69
|
|
|
$query->{$scope}(...$arguments); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $query; |
|
73
|
|
|
}; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Adds a macro to the query builder. |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $name |
|
80
|
|
|
* @return void |
|
81
|
|
|
*/ |
|
82
|
|
|
public static function addMacro(string $name = 'whereNot') |
|
83
|
|
|
{ |
|
84
|
|
|
Builder::macro($name, function ($withQuery): Builder { |
|
85
|
|
|
$callable = is_callable($withQuery) |
|
86
|
|
|
? $withQuery |
|
87
|
|
|
: WhereNot::makeCallable($withQuery); |
|
88
|
|
|
|
|
89
|
|
|
// We do this to make sure the $query variable is an Eloquent Query Builder. |
|
90
|
|
|
$builder = WhereNot::builder($this); |
|
91
|
|
|
|
|
92
|
|
|
return $builder->whereNotExists(function ($query) use ($callable, $builder) { |
|
93
|
|
|
// Create a new Eloquent Query Builder with the given Query Builder and |
|
94
|
|
|
// set the model from the original builder. |
|
95
|
|
|
$query = new Builder($query); |
|
96
|
|
|
$query->setModel($model = $builder->getModel()); |
|
97
|
|
|
|
|
98
|
|
|
$qualifiedKeyName = $model->getQualifiedKeyName(); |
|
99
|
|
|
$originalTable = $model->getTable(); |
|
100
|
|
|
|
|
101
|
|
|
// Instantiate a new model that uses the aliased table. |
|
102
|
|
|
$aliasedTable = WhereNot::getTableAlias($originalTable); |
|
103
|
|
|
$aliasedModel = $query->newModelInstance()->setTable($aliasedTable); |
|
104
|
|
|
|
|
105
|
|
|
// Apply the where constraint based on the model's key name and apply the $callable. |
|
106
|
|
|
$query |
|
107
|
|
|
->setModel($aliasedModel) |
|
108
|
|
|
->select(DB::raw(1)) |
|
109
|
|
|
->from($originalTable, $aliasedTable) |
|
110
|
|
|
->whereColumn($aliasedModel->getQualifiedKeyName(), $qualifiedKeyName) |
|
111
|
|
|
->limit(1) |
|
112
|
|
|
->tap(fn ($query) => $callable($query)); |
|
|
|
|
|
|
113
|
|
|
}); |
|
114
|
|
|
}); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|