Skip to content

Commit

Permalink
Convert Expression to string for from in having subqueries (#48525)
Browse files Browse the repository at this point in the history
  • Loading branch information
ikari7789 authored Sep 28, 2023
1 parent 6014dd4 commit 4b22e39
Show file tree
Hide file tree
Showing 2 changed files with 20 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()->grammar->getValue($from->getQuery()->from),
$this->getModel()->getTable()
) : $from->getQuery()->wheres;

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

public function testHasWithConstraintsWithOrWhereAndSubqueryInRelationFromClause()
{
EloquentBuilderTestModelParentStub::resolveRelationUsing('addressAsExpression', function ($model) {
return $model->address()->fromSub(EloquentBuilderTestModelCloseRelatedStub::query(), 'eloquent_builder_test_model_close_related_stubs');
});

$model = new EloquentBuilderTestModelParentStub;

$builder = $model->where('name', 'larry');
$builder->whereHas('addressAsExpression', function ($q) {
$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 "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 4b22e39

Please sign in to comment.