Skip to content

[12.x] Ensure automatic eager loading works in creating events #56193

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

Closed
wants to merge 5 commits into from
Closed
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
4 changes: 4 additions & 0 deletions src/Illuminate/Database/Eloquent/Concerns/HasEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ protected function fireModelEvent($event, $halt = true)
return true;
}

if (static::isAutomaticallyEagerLoadingRelationships() && $event == 'creating') {
$this->setRelations(array_filter($this->getRelations()));
}

// First, we will get the proper method to call on the event dispatcher, and then we
// will attempt to fire a custom, object based event for the given event. If that
// returns a result we can return that result, or we'll call the string events.
Expand Down
76 changes: 76 additions & 0 deletions tests/Integration/Database/EloquentModelRelationAutoloadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Illuminate\Tests\Integration\Database\EloquentModelRelationAutoloadTest;

use DB;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Expand All @@ -12,6 +14,13 @@ class EloquentModelRelationAutoloadTest extends DatabaseTestCase
{
protected function afterRefreshingDatabase()
{
Schema::create('tags', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->nullable();
$table->string('status')->nullable();
$table->unsignedInteger('post_id')->nullable();
});

Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
});
Expand Down Expand Up @@ -214,6 +223,63 @@ public function testRelationAutoloadVariousNestedMorphRelations()

DB::disableQueryLog();
}

public function testRelationAutoloadWorksOnCreatingEvent()
{
Model::automaticallyEagerLoadRelationships();

DB::enableQueryLog();

$tags = Tag::factory()->times(3)->make();

$post = Post::create();

$post->tags()->saveMany($tags);

$this->assertCount(7, DB::getQueryLog());

Model::automaticallyEagerLoadRelationships(false);

DB::disableQueryLog();
}
}

class TagFactory extends Factory
{
protected $model = Tag::class;

public function definition()
{
return [];
}
}

class Tag extends Model
{
use HasFactory;

public $timestamps = false;

protected $guarded = [];

protected static function booted()
{
static::creating(function ($model) {
if ($model->post->shouldApplyStatus()) {
$model->status = 'Todo';
}
});
}

protected static function newFactory()
{
return TagFactory::new();
}

public function post()
{
return $this->belongsTo(Post::class);
}
}

class Comment extends Model
Expand Down Expand Up @@ -242,6 +308,11 @@ class Post extends Model
{
public $timestamps = false;

public function shouldApplyStatus()
{
return false;
}

public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
Expand All @@ -256,6 +327,11 @@ public function likes()
{
return $this->morphMany(Like::class, 'likeable');
}

public function tags()
{
return $this->hasMany(Tag::class);
}
}

class Video extends Model
Expand Down