-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[7.x] Add 'loadMorph' and 'loadMorphCount' on Eloquent Model (#32760)
* 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
1 parent
dce4429
commit dcf2d33
Showing
3 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
tests/Integration/Database/EloquentMorphCountLazyEagerLoadingTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
78
tests/Integration/Database/EloquentMorphLazyEagerLoadingTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |