Skip to content

Commit

Permalink
Where in where not in (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
fico7489 authored Feb 8, 2019
1 parent 487abd9 commit a18830f
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 2 deletions.
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,29 @@ Options are : **SUM**, **AVG**, **MAX**, **MIN**, **COUNT**

**whereJoin($column, $operator, $value, $boolean = 'and')**

* ***$column***, ***$operator***, ***$value*** and ***$boolean*** arguments are the same as in default eloquent **where()**
* arguments are the same as in default eloquent **where()**

**orWhereJoin($column, $operator, $value)**

* ***$column***, ***$operator*** and ***$value*** arguments are the same as in default eloquent **orWhere()**
* arguments are the same as in default eloquent **orWhere()**


**whereInJoin($column, $values, $boolean = 'and', $not = false)**

* arguments are the same as in default eloquent **whereIn()**

**whereNotInJoin($column, $values, $boolean = 'and')**

* arguments are the same as in default eloquent **whereNotIn()**

**orWhereInJoin($column, $values)**

* arguments are the same as in default eloquent **orWhereIn()**

**orWhereNotInJoin($column, $values, $boolean = 'and')**

* arguments are the same as in default eloquent **orWhereNotIn()**


### Allowed clauses on BelongsTo, HasOne and HasMany relations on which you can use join clauses on the query

Expand Down
34 changes: 34 additions & 0 deletions src/EloquentJoinBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class EloquentJoinBuilder extends Builder
//store clauses on relation for join
public $relationClauses = [];

//query methods
public function where($column, $operator = null, $value = null, $boolean = 'and')
{
if ($column instanceof \Closure) {
Expand Down Expand Up @@ -81,6 +82,38 @@ public function orWhereJoin($column, $operator, $value)
return $this->orWhere($column, $operator, $value);
}

public function whereInJoin($column, $values, $boolean = 'and', $not = false)
{
$query = $this->baseBuilder ? $this->baseBuilder : $this;
$column = $query->performJoin($column);

return $this->whereIn($column, $values, $boolean, $not);
}

public function whereNotInJoin($column, $values, $boolean = 'and')
{
$query = $this->baseBuilder ? $this->baseBuilder : $this;
$column = $query->performJoin($column);

return $this->whereNotIn($column, $values, $boolean);
}

public function orWhereInJoin($column, $values)
{
$query = $this->baseBuilder ? $this->baseBuilder : $this;
$column = $query->performJoin($column);

return $this->whereIn($column, $values, 'or');
}

public function orWhereNotInJoin($column, $values, $boolean = 'and')
{
$query = $this->baseBuilder ? $this->baseBuilder : $this;
$column = $query->performJoin($column);

return $this->whereIn($column, $values, $boolean, true);
}

public function orderByJoin($column, $direction = 'asc', $aggregateMethod = null)
{
$dotPos = strrpos($column, '.');
Expand Down Expand Up @@ -115,6 +148,7 @@ public function joinRelations($relations, $leftJoin = null)
return $this;
}

//helpers methods
private function performJoin($relations, $leftJoin = null)
{
//detect join method
Expand Down
28 changes: 28 additions & 0 deletions tests/Tests/Clauses/OrWhereInTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Fico7489\Laravel\EloquentJoin\Tests\Tests\Clauses;

use Fico7489\Laravel\EloquentJoin\Tests\Models\Order;
use Fico7489\Laravel\EloquentJoin\Tests\TestCase;

class OrWhereInTest extends TestCase
{
public function testWhere()
{
Order::joinRelations('seller')
->whereInJoin('seller.id', [1, 2])
->orWhereInJoin('seller.id', [3, 4])
->get();

$queryTest = 'select orders.*
from "orders"
left join "sellers" on "sellers"."id" = "orders"."seller_id"
where ("sellers"."id" in (?, ?)
or
"sellers"."id" in (?, ?))
and "orders"."deleted_at" is null
group by "orders"."id"';

$this->assertQueryMatches($queryTest, $this->fetchQuery());
}
}
27 changes: 27 additions & 0 deletions tests/Tests/Clauses/OrWhereNotInTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Fico7489\Laravel\EloquentJoin\Tests\Tests\Clauses;

use Fico7489\Laravel\EloquentJoin\Tests\Models\Order;
use Fico7489\Laravel\EloquentJoin\Tests\TestCase;

class OrWhereNotInTest extends TestCase
{
public function testWhere()
{
Order::joinRelations('seller')
->whereInJoin('seller.id', [1, 2])
->orWhereNotInJoin('seller.id', [3, 4])
->get();

$queryTest = 'select orders.*
from "orders"
left join "sellers" on "sellers"."id" = "orders"."seller_id"
where "sellers"."id" in (?, ?)
and "sellers"."id" not in (?, ?)
and "orders"."deleted_at" is null
group by "orders"."id"';

$this->assertQueryMatches($queryTest, $this->fetchQuery());
}
}
25 changes: 25 additions & 0 deletions tests/Tests/Clauses/WhereInTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Fico7489\Laravel\EloquentJoin\Tests\Tests\Clauses;

use Fico7489\Laravel\EloquentJoin\Tests\Models\Order;
use Fico7489\Laravel\EloquentJoin\Tests\TestCase;

class WhereInTest extends TestCase
{
public function testWhere()
{
Order::joinRelations('seller')
->whereInJoin('seller.id', [1, 2])
->get();

$queryTest = 'select orders.*
from "orders"
left join "sellers" on "sellers"."id" = "orders"."seller_id"
where "sellers"."id" in (?, ?)
and "orders"."deleted_at" is null
group by "orders"."id"';

$this->assertQueryMatches($queryTest, $this->fetchQuery());
}
}
25 changes: 25 additions & 0 deletions tests/Tests/Clauses/WhereNotInTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Fico7489\Laravel\EloquentJoin\Tests\Tests\Clauses;

use Fico7489\Laravel\EloquentJoin\Tests\Models\Order;
use Fico7489\Laravel\EloquentJoin\Tests\TestCase;

class WhereNotInTest extends TestCase
{
public function testWhere()
{
Order::joinRelations('seller')
->whereNotInJoin('seller.id', [1, 2])
->get();

$queryTest = 'select orders.*
from "orders"
left join "sellers" on "sellers"."id" = "orders"."seller_id"
where "sellers"."id" not in (?, ?)
and "orders"."deleted_at" is null
group by "orders"."id"';

$this->assertQueryMatches($queryTest, $this->fetchQuery());
}
}

0 comments on commit a18830f

Please sign in to comment.