Skip to content

Commit

Permalink
Convert Expression to string for from in having subqueries
Browse files Browse the repository at this point in the history
  • Loading branch information
ikari7789 authored and andrew-miller-rakuten committed Sep 25, 2023
1 parent 124eeae commit fc01137
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ public function mergeConstraintsFrom(Builder $from)
$wheres = $from->getQuery()->from !== $this->getQuery()->from
? $this->requalifyWhereTables(
$from->getQuery()->wheres,
$from->getQuery()->from,
$from->getQuery()->from instanceof Expression ? $from->getQuery()->from->getValue($from->getQuery()->grammer) : $from->getQuery()->from,
$this->getModel()->getTable()
) : $from->getQuery()->wheres;

Expand Down
16 changes: 16 additions & 0 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1460,6 +1460,22 @@ public function testHasWithConstraintsWithOrWhereAndHavingInSubquery()
$this->assertEquals(['larry', '90210', '90220', 'fooside dr', 29], $builder->getBindings());
}

public function testHasWithConstraintsWithOrWhereAndSubqueryInFromInSubquery()
{
$model = new EloquentBuilderTestModelParentStub;

$builder = $model->where('name', 'larry');
$builder->whereHas('address', function ($q) {
$q->fromSub(EloquentBuilderTestModelCloseRelatedStub::query(), 'eloquent_builder_test_model_close_related_stubs');
$q->where('zipcode', '90210');
$q->orWhere('zipcode', '90220');
$q->having('street', '=', 'fooside dr');
})->where('age', 29);

$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where "name" = ? and exists (select * from (select * from "eloquent_builder_test_model_close_related_stubs") as "eloquent_builder_test_model_close_related_stubs" where "eloquent_builder_test_model_parent_stubs"."foo_id" = "eloquent_builder_test_model_close_related_stubs"."id" and ("zipcode" = ? or "zipcode" = ?) having "street" = ?) and "age" = ?', $builder->toSql());
$this->assertEquals(['larry', '90210', '90220', 'fooside dr', 29], $builder->getBindings());
}

public function testHasWithConstraintsAndJoinAndHavingInSubquery()
{
$model = new EloquentBuilderTestModelParentStub;
Expand Down

0 comments on commit fc01137

Please sign in to comment.