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

[7.x] Add 'loadMorph' and 'loadMorphCount' on Eloquent Model #32760

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,22 @@ public function load($relations)
return $this;
}

/**
* Eager load relationships on the polymorphic relation of a model.
*
* @param string $relation
* @param array $relations
* @return $this
*/
public function loadMorph($relation, $relations)
{
$className = get_class($this->{$relation});

$this->{$relation}->load($relations[$className] ?? []);

return $this;
}

/**
* Eager load relations on the model if they are not already eager loaded.
*
Expand Down Expand Up @@ -550,6 +566,22 @@ public function loadCount($relations)
return $this;
}

/**
* Eager load relationship counts on the polymorphic relation of a model.
*
* @param string $relation
* @param array $relations
* @return $this
*/
public function loadMorphCount($relation, $relations)
{
$className = get_class($this->{$relation});

$this->{$relation}->loadCount($relations[$className] ?? []);

return $this;
}

/**
* Increment a column's value by a given amount.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Illuminate\Tests\Integration\Database\EloquentMorphCountLazyEagerLoadingTest;

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

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

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

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

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

$post = Post::create();

tap((new Like)->post()->associate($post))->save();
tap((new Like)->post()->associate($post))->save();

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

public function testLazyEagerLoading()
{
$comment = Comment::first();

$comment->loadMorphCount('commentable', [
Post::class => ['likes'],
]);

$this->assertTrue($comment->relationLoaded('commentable'));
$this->assertEquals(2, $comment->commentable->likes_count);
}
}

class Comment extends Model
{
public $timestamps = false;

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

class Post extends Model
{
public $timestamps = false;

public function likes()
{
return $this->hasMany(Like::class);
}
}

class Like extends Model
{
public $timestamps = false;

public function post()
{
return $this->belongsTo(Post::class);
}
}
78 changes: 78 additions & 0 deletions tests/Integration/Database/EloquentMorphLazyEagerLoadingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Illuminate\Tests\Integration\Database\EloquentMorphLazyEagerLoadingTest;

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

/**
* @group integration
*/
class EloquentMorphLazyEagerLoadingTest 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('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();

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

public function testLazyEagerLoading()
{
$comment = Comment::first();

$comment->loadMorph('commentable', [
Post::class => ['user'],
]);

$this->assertTrue($comment->relationLoaded('commentable'));
$this->assertTrue($comment->commentable->relationLoaded('user'));
}
}

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;
}