Skip to content

Commit

Permalink
[7.x] Add 'loadMorph' and 'loadMorphCount' on Eloquent Model (#32760)
Browse files Browse the repository at this point in the history
* Add 'loadMorph' method to Eloquent Model

* Add 'loadMorphCount' method to Eloquent model

* Fix StyleCI issue

Co-authored-by: GuntherDebrauwer <22586858+GuntherDebrauwer@users.noreply.github.com>
  • Loading branch information
gdebrauwer and gdebrauwer committed May 11, 2020
1 parent dce4429 commit dcf2d33
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 0 deletions.
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;
}

0 comments on commit dcf2d33

Please sign in to comment.