Skip to content

Commit

Permalink
增加whereHasNotIn
Browse files Browse the repository at this point in the history
  • Loading branch information
jqhph committed Jul 29, 2021
1 parent bc596dc commit 4efe5c3
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/.ide-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
/**
* @method $this whereHasIn(string $relation, ?\Closure $callable = null)
* @method $this orWhereHasIn(string $relation, ?\Closure $callable = null)
* @method $this whereHasNotIn(string $relation, ?\Closure $callable = null)
* @method $this orWhereHasNotIn(string $relation, ?\Closure $callable = null)
* @method $this whereHasMorphIn(string $relation, $types, ?\Closure $callable = null)
* @method $this orWhereHasMorphIn(string $relation, $types, ?\Closure $callable = null)
*/
class Builder
class Builder extends \Illuminate\Database\Query\Builder
{
}
}
22 changes: 14 additions & 8 deletions src/Builder/WhereHasIn.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class WhereHasIn
*/
protected $callback;

/**
* @var string
*/
protected $method = 'whereIn';

public function __construct(Eloquent\Builder $builder, $relation, $callback)
{
$this->builder = $builder;
Expand All @@ -46,7 +51,7 @@ public function execute()
return $this->builder;
}

return $this->whereIn(
return $this->where(
$this->formatRelation()
);
}
Expand All @@ -58,19 +63,20 @@ public function execute()
*
* @throws \Exception
*/
protected function whereIn($relation)
protected function where($relation)
{
if ($relation instanceof Relations\MorphTo) {
throw new \Exception('Please use whereHasMorphIn() for MorphTo relationships.');
}

$relationQuery = $this->getRelationQuery($relation);
$method = $this->method;

if (
$relation instanceof Relations\MorphOne
|| $relation instanceof Relations\MorphMany
) {
return $this->builder->whereIn(
return $this->builder->{$method}(
$relation->getQualifiedParentKeyName(),
$this->withRelationQueryCallback(
$relationQuery
Expand All @@ -82,7 +88,7 @@ protected function whereIn($relation)
}

if ($relation instanceof Relations\MorphToMany) {
return $this->builder->whereIn(
return $this->builder->{$method}(
$relation->getQualifiedParentKeyName(),
$this->withRelationQueryCallback(
$relationQuery
Expand All @@ -95,7 +101,7 @@ protected function whereIn($relation)

// BelongsTo
if ($relation instanceof Relations\BelongsTo) {
return $this->builder->whereIn(
return $this->builder->{$method}(
$this->getRelationQualifiedForeignKeyName($relation),
$this->withRelationQueryCallback(
$relationQuery
Expand All @@ -109,7 +115,7 @@ protected function whereIn($relation)
$relation instanceof Relations\HasOne
|| $relation instanceof Relations\HasMany
) {
return $this->builder->whereIn(
return $this->builder->{$method}(
$relation->getQualifiedParentKeyName(),
$this->withRelationQueryCallback(
$relationQuery
Expand All @@ -121,7 +127,7 @@ protected function whereIn($relation)

// BelongsToMany
if ($relation instanceof Relations\BelongsToMany) {
return $this->builder->whereIn(
return $this->builder->{$method}(
$relation->getQualifiedParentKeyName(),
$this->withRelationQueryCallback(
$relationQuery
Expand All @@ -135,7 +141,7 @@ protected function whereIn($relation)
$relation instanceof Relations\HasOneThrough
|| $relation instanceof Relations\HasManyThrough
) {
return $this->builder->whereIn(
return $this->builder->{$method}(
$relation->getQualifiedLocalKeyName(),
$this->withRelationQueryCallback(
$relationQuery
Expand Down
11 changes: 11 additions & 0 deletions src/Builder/WhereHasNotIn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Dcat\Laravel\Database\Builder;

class WhereHasNotIn extends WhereHasIn
{
/**
* @var string
*/
protected $method = 'whereNotIn';
}
20 changes: 20 additions & 0 deletions src/WhereHasInServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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;

Expand All @@ -30,6 +31,25 @@ public function register()
});
});

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');
Expand Down

0 comments on commit 4efe5c3

Please sign in to comment.