Skip to content

Commit 1676c2b

Browse files
author
admin
committed
Add support whereRawJoin and orWhereRawJoin
1 parent bf59ca1 commit 1676c2b

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

src/EloquentJoinBuilder.php

+36
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,26 @@ public function orWhereJoin($column, $operator, $value)
8787
return $this->orWhere($column, $operator, $value);
8888
}
8989

90+
public function whereRawJoin($column, $sql, $bindings = [], $boolean = 'and')
91+
{
92+
$query = $this->baseBuilder ? $this->baseBuilder : $this;
93+
$column = $query->performJoin($column);
94+
95+
$raw = $this->replaceColumnKey($sql, $column);
96+
97+
return $this->whereRaw($raw, $bindings, $boolean);
98+
}
99+
100+
public function orWhereRawJoin($column, $sql, $bindings = [], $boolean = 'and')
101+
{
102+
$query = $this->baseBuilder ? $this->baseBuilder : $this;
103+
$column = $query->performJoin($column);
104+
105+
$raw = $this->replaceColumnKey($sql, $column);
106+
107+
return $this->orWhereRaw($raw, $bindings, $boolean);
108+
}
109+
90110
public function whereInJoin($column, $values, $boolean = 'and', $not = false)
91111
{
92112
$query = $this->baseBuilder ? $this->baseBuilder : $this;
@@ -290,6 +310,22 @@ protected function joinQuery($join, $relation, $relatedTableAlias)
290310
}
291311
}
292312

313+
private function replaceColumnKey($sql, $column)
314+
{
315+
return str_replace(':column', $this->prepareColumn($column), $sql);
316+
}
317+
318+
private function prepareColumn($column)
319+
{
320+
$parts = explode('.', $column);
321+
322+
$parts = array_map(function ($value) {
323+
return '"'.$value.'"';
324+
}, $parts);
325+
326+
return implode('.', $parts);
327+
}
328+
293329
private function applyClauseOnRelation(JoinClause $join, string $method, array $params, string $relatedTableAlias)
294330
{
295331
if (in_array($method, ['where', 'orWhere'])) {
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Fico7489\Laravel\EloquentJoin\Tests\Tests\Clauses;
4+
5+
use Fico7489\Laravel\EloquentJoin\Tests\Models\Order;
6+
use Fico7489\Laravel\EloquentJoin\Tests\TestCase;
7+
8+
class OrWhereRawTest extends TestCase
9+
{
10+
public function testWhere()
11+
{
12+
Order::joinRelations('seller')
13+
->whereRawJoin('seller.id', ':column = ?', [1])
14+
->orWhereRawJoin('seller.id', ':column = ?', [2])
15+
->get();
16+
17+
$queryTest = 'select orders.*
18+
from "orders"
19+
left join "sellers" on "sellers"."id" = "orders"."seller_id"
20+
where ("sellers"."id" = 1 or "sellers"."id" = 2)
21+
and "orders"."deleted_at" is null
22+
group by "orders"."id"';
23+
24+
$this->assertQueryMatches($queryTest, $this->fetchQuery());
25+
}
26+
}

tests/Tests/Clauses/WhereRawTest.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Fico7489\Laravel\EloquentJoin\Tests\Tests\Clauses;
4+
5+
use Fico7489\Laravel\EloquentJoin\Tests\Models\Order;
6+
use Fico7489\Laravel\EloquentJoin\Tests\TestCase;
7+
8+
class WhereRawTest extends TestCase
9+
{
10+
public function testWhere()
11+
{
12+
Order::joinRelations('seller')
13+
->whereRawJoin('seller.id', ':column = ?', [1])
14+
->get();
15+
16+
$queryTest = 'select orders.*
17+
from "orders"
18+
left join "sellers" on "sellers"."id" = "orders"."seller_id"
19+
where "sellers"."id" = 1
20+
and "orders"."deleted_at" is null
21+
group by "orders"."id"';
22+
23+
$this->assertQueryMatches($queryTest, $this->fetchQuery());
24+
}
25+
}

0 commit comments

Comments
 (0)