WhereHasInServiceProvider.php
2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
namespace Dcat\Laravel\Database;
use Dcat\Laravel\Database\Builder\WhereHasIn;
use Dcat\Laravel\Database\Builder\WhereHasMorphIn;
use Dcat\Laravel\Database\Builder\WhereHasNotIn;
use Illuminate\Database\Eloquent;
use Illuminate\Support\ServiceProvider;
class WhereHasInServiceProvider extends ServiceProvider
{
public function register()
{
Eloquent\Builder::macro('whereHasIn', function ($relationName, $callable = null) {
return (new WhereHasIn($this, $relationName, function ($nextRelation, $builder) use ($callable) {
if ($nextRelation) {
return $builder->whereHasIn($nextRelation, $callable);
}
if ($callable) {
return $builder->callScope($callable);
}
return $builder;
}))->execute();
});
Eloquent\Builder::macro('orWhereHasIn', function ($relationName, $callable = null) {
return $this->orWhere(function ($query) use ($relationName, $callable) {
return $query->whereHasIn($relationName, $callable);
});
});
Eloquent\Builder::macro('whereHasNotIn', function ($relationName, $callable = null) {
return (new WhereHasNotIn($this, $relationName, function ($nextRelation, $builder) use ($callable) {
if ($nextRelation) {
return $builder->whereHasNotIn($nextRelation, $callable);
}
if ($callable) {
return $builder->callScope($callable);
}
return $builder;
}))->execute();
});
Eloquent\Builder::macro('orWhereHasNotIn', function ($relationName, $callable = null) {
return $this->orWhere(function ($query) use ($relationName, $callable) {
return $query->whereHasNotIn($relationName, $callable);
});
});
Eloquent\Builder::macro('whereHasMorphIn', WhereHasMorphIn::make());
Eloquent\Builder::macro('orWhereHasMorphIn', function ($relation, $types, $callback = null) {
return $this->whereHasMorphIn($relation, $types, $callback, 'or');
});
}
}