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

[5.8] Add support for typed eager loads #28647

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/Illuminate/Database/Eloquent/Relations/MorphTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ class MorphTo extends BelongsTo
*/
protected $macroBuffer = [];

/**
* A map of relations to load for each individual morph type.
*
* @var array
*/
protected $typedEagerLoads = [];

/**
* Create a new morph to relationship instance.
*
Expand Down Expand Up @@ -111,7 +118,10 @@ protected function getResultsByType($type)

$query = $this->replayMacros($instance->newQuery())
->mergeConstraintsFrom($this->getQuery())
->with($this->getQuery()->getEagerLoads());
->with(array_merge(
$this->getQuery()->getEagerLoads(),
$this->typedEagerLoads[get_class($instance)] ?? []
));

return $query->whereIn(
$instance->getTable().'.'.$ownerKey, $this->gatherKeysByType($type)
Expand Down Expand Up @@ -253,6 +263,24 @@ public function getDictionary()
return $this->dictionary;
}

/**
* Specify which relations to load for a given morph type.
*
* @param string $modelClass
brendt marked this conversation as resolved.
Show resolved Hide resolved
* @param array $with
brendt marked this conversation as resolved.
Show resolved Hide resolved
*
brendt marked this conversation as resolved.
Show resolved Hide resolved
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function withMorph(string $modelClass, array $with): self
brendt marked this conversation as resolved.
Show resolved Hide resolved
{
$this->typedEagerLoads[$modelClass] = array_merge(
$this->typedEagerLoads[$modelClass] ?? [],
$with
);

return $this;
}

/**
* Replay stored macro calls on the actual related instance.
*
Expand Down
94 changes: 94 additions & 0 deletions tests/Integration/Database/EloquentMorphEagerLoadingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace Illuminate\Tests\Integration\Database\EloquentMorphToLazyEagerLoadingTest;

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

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

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

Schema::create('posts', function (Blueprint $table) {
$table->increments('post_id');
$table->unsignedInteger('user_id');
});

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

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

$user = User::create();

$post = tap((new Post)->user()->associate($user))->save();

$video = Video::create();

(new Comment)->commentable()->associate($post)->save();
(new Comment)->commentable()->associate($video)->save();
}

public function test_with_morph_loading()
{
$comments = Comment::query()
->with(['commentable' => function (MorphTo $morphTo) {
$morphTo->withMorph(Post::class, ['user']);
}])
->get();

$this->assertTrue($comments[0]->relationLoaded('commentable'));
$this->assertTrue($comments[0]->commentable->relationLoaded('user'));
$this->assertTrue($comments[1]->relationLoaded('commentable'));
}
}

class Comment extends Model
{
public $timestamps = false;

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

class Post extends Model
{
public $timestamps = false;
protected $primaryKey = 'post_id';

public function user()
{
return $this->belongsTo(User::class);
}
}

class User extends Model
{
public $timestamps = false;
}

class Video extends Model
{
public $timestamps = false;
protected $primaryKey = 'video_id';
}