Skip to content

Commit

Permalink
[8.x] Fix one-of-many bindings (laravel#37616)
Browse files Browse the repository at this point in the history
* fix

* test

* formatting

* formatting

* formatting
  • Loading branch information
cbl authored Jun 7, 2021
1 parent 4edad7b commit 0613654
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ protected function newOneOfManySubQuery($groupBy, $column = null, $aggregate = n
protected function addOneOfManyJoinSubQuery(Builder $parent, Builder $subQuery, $on)
{
$parent->beforeQuery(function ($parent) use ($subQuery, $on) {
$subQuery->applyBeforeQueryCallbacks();

$parent->joinSub($subQuery, $this->relationName, function ($join) use ($on) {
$join->on($this->qualifySubSelectColumn($on), '=', $this->qualifyRelatedColumn($on));

Expand Down
24 changes: 24 additions & 0 deletions tests/Database/DatabaseEloquentHasOneOfManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\SoftDeletes;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -41,6 +42,7 @@ public function createSchema()
$this->schema()->create('logins', function ($table) {
$table->increments('id');
$table->foreignId('user_id');
$table->dateTime('deleted_at')->nullable();
});

$this->schema()->create('states', function ($table) {
Expand Down Expand Up @@ -379,6 +381,14 @@ public function testWithExistsWithConstraintsInJoinSubSelect()
$this->assertTrue($user->foo_state_exists);
}

public function testWithSoftDeletes()
{
$user = HasOneOfManyTestUser::create();
$user->logins()->create();
$user->latest_login_with_soft_deletes;
$this->assertNotNull($user->latest_login_with_soft_deletes);
}

/**
* Get a database connection instance.
*
Expand Down Expand Up @@ -419,6 +429,11 @@ public function latest_login()
return $this->hasOne(HasOneOfManyTestLogin::class, 'user_id')->ofMany();
}

public function latest_login_with_soft_deletes()
{
return $this->hasOne(HasOneOfManyTestLoginWithSoftDeletes::class, 'user_id')->ofMany();
}

public function latest_login_with_shortcut()
{
return $this->hasOne(HasOneOfManyTestLogin::class, 'user_id')->latestOfMany();
Expand Down Expand Up @@ -490,6 +505,15 @@ class HasOneOfManyTestLogin extends Eloquent
public $timestamps = false;
}

class HasOneOfManyTestLoginWithSoftDeletes extends Eloquent
{
use SoftDeletes;

protected $table = 'logins';
protected $guarded = [];
public $timestamps = false;
}

class HasOneOfManyTestState extends Eloquent
{
protected $table = 'states';
Expand Down

0 comments on commit 0613654

Please sign in to comment.