Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] MorphTo relationship eager loading constraints #35190

Merged
merged 2 commits into from Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/MorphTo.php
Expand Up @@ -51,6 +51,13 @@ class MorphTo extends BelongsTo
*/
protected $morphableEagerLoadCounts = [];

/**
* A map of constraints to apply for each individual morph type.
*
* @var array
*/
protected $morphableConstraints = [];

/**
* Create a new morph to relationship instance.
*
Expand Down Expand Up @@ -133,6 +140,10 @@ protected function getResultsByType($type)
(array) ($this->morphableEagerLoadCounts[get_class($instance)] ?? [])
);

if ($callback = $this->morphableConstraints[get_class($instance)] ?? null) {
$callback($query);
}

$whereIn = $this->whereInMethod($instance, $ownerKey);

return $query->{$whereIn}(
Expand Down Expand Up @@ -312,6 +323,21 @@ public function morphWithCount(array $withCount)
return $this;
}

/**
* Specify constraints on the query for a given morph type.
*
* @param array $with
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function constrain(array $callbacks)
{
$this->morphableConstraints = array_merge(
$this->morphableConstraints, $callbacks
);

return $this;
}

/**
* Replay stored macro calls on the actual related instance.
*
Expand Down
93 changes: 93 additions & 0 deletions tests/Integration/Database/EloquentMorphConstrainTest.php
@@ -0,0 +1,93 @@
<?php

namespace Illuminate\Tests\Integration\Database\EloquentMorphConstrainTest;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Tests\Integration\Database\DatabaseTestCase;

/**
* @group integration
*/
class EloquentMorphConstrainTest extends DatabaseTestCase
{
protected function setUp(): void
{
parent::setUp();

Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->boolean('post_visible');
});

Schema::create('videos', function (Blueprint $table) {
$table->increments('id');
$table->boolean('video_visible');
});

Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->string('commentable_type');
$table->integer('commentable_id');
});

$post1 = Post::create(['post_visible' => true]);
(new Comment)->commentable()->associate($post1)->save();

$post2 = Post::create(['post_visible' => false]);
(new Comment)->commentable()->associate($post2)->save();

$video1 = Video::create(['video_visible' => true]);
(new Comment)->commentable()->associate($video1)->save();

$video2 = Video::create(['video_visible' => false]);
(new Comment)->commentable()->associate($video2)->save();
}

public function testMorphConstraints()
{
$comments = Comment::query()
->with(['commentable' => function (MorphTo $morphTo) {
$morphTo->constrain([
Post::class => function ($query) {
$query->where('post_visible', true);
},
Video::class => function ($query) {
$query->where('video_visible', true);
},
]);
}])
->get();

$this->assertTrue($comments[0]->commentable->post_visible);
$this->assertNull($comments[1]->commentable);
$this->assertTrue($comments[2]->commentable->video_visible);
$this->assertNull($comments[3]->commentable);
}
}

class Comment extends Model
{
public $timestamps = false;

public function commentable()
{
return $this->morphTo();
}
}

class Post extends Model
{
public $timestamps = false;
protected $fillable = ['post_visible'];
protected $casts = ['post_visible' => 'boolean'];
}

class Video extends Model
{
public $timestamps = false;
protected $fillable = ['video_visible'];
protected $casts = ['video_visible' => 'boolean'];
}