From 0c23d136fd314707d5cc4e6679b1d3be4524b1c9 Mon Sep 17 00:00:00 2001 From: GuntherDebrauwer <22586858+GuntherDebrauwer@users.noreply.github.com> Date: Mon, 11 May 2020 20:33:12 +0200 Subject: [PATCH 1/3] Add 'loadMorph' method to Eloquent Model --- src/Illuminate/Database/Eloquent/Model.php | 16 ++++ .../EloquentMorphLazyEagerLoadingTest.php | 78 +++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 tests/Integration/Database/EloquentMorphLazyEagerLoadingTest.php diff --git a/src/Illuminate/Database/Eloquent/Model.php b/src/Illuminate/Database/Eloquent/Model.php index daa6e6d3a30d..0a45103c81fe 100644 --- a/src/Illuminate/Database/Eloquent/Model.php +++ b/src/Illuminate/Database/Eloquent/Model.php @@ -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. * diff --git a/tests/Integration/Database/EloquentMorphLazyEagerLoadingTest.php b/tests/Integration/Database/EloquentMorphLazyEagerLoadingTest.php new file mode 100644 index 000000000000..fde0da3f6723 --- /dev/null +++ b/tests/Integration/Database/EloquentMorphLazyEagerLoadingTest.php @@ -0,0 +1,78 @@ +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; +} From 10ce8c16f6920a98ccd1629118005f8c93e6b02e Mon Sep 17 00:00:00 2001 From: GuntherDebrauwer <22586858+GuntherDebrauwer@users.noreply.github.com> Date: Mon, 11 May 2020 20:33:29 +0200 Subject: [PATCH 2/3] Add 'loadMorphCount' method to Eloquent model --- src/Illuminate/Database/Eloquent/Model.php | 16 ++++ ...EloquentMorphCountLazyEagerLoadingTest.php | 83 +++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 tests/Integration/Database/EloquentMorphCountLazyEagerLoadingTest.php diff --git a/src/Illuminate/Database/Eloquent/Model.php b/src/Illuminate/Database/Eloquent/Model.php index 0a45103c81fe..6efc6d847025 100644 --- a/src/Illuminate/Database/Eloquent/Model.php +++ b/src/Illuminate/Database/Eloquent/Model.php @@ -566,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. * diff --git a/tests/Integration/Database/EloquentMorphCountLazyEagerLoadingTest.php b/tests/Integration/Database/EloquentMorphCountLazyEagerLoadingTest.php new file mode 100644 index 000000000000..3597403f2725 --- /dev/null +++ b/tests/Integration/Database/EloquentMorphCountLazyEagerLoadingTest.php @@ -0,0 +1,83 @@ +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); + } +} From bc846141c50c9af84f68b97595d3bd2f5d167aca Mon Sep 17 00:00:00 2001 From: GuntherDebrauwer <22586858+GuntherDebrauwer@users.noreply.github.com> Date: Mon, 11 May 2020 20:43:08 +0200 Subject: [PATCH 3/3] Fix StyleCI issue --- .../Database/EloquentMorphCountLazyEagerLoadingTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Integration/Database/EloquentMorphCountLazyEagerLoadingTest.php b/tests/Integration/Database/EloquentMorphCountLazyEagerLoadingTest.php index 3597403f2725..3335421fc942 100644 --- a/tests/Integration/Database/EloquentMorphCountLazyEagerLoadingTest.php +++ b/tests/Integration/Database/EloquentMorphCountLazyEagerLoadingTest.php @@ -44,7 +44,7 @@ public function testLazyEagerLoading() $comment = Comment::first(); $comment->loadMorphCount('commentable', [ - Post::class => ['likes'] + Post::class => ['likes'], ]); $this->assertTrue($comment->relationLoaded('commentable'));