Skip to content

[10.x] Set morph type for MorphToMany pivot model #48432

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

Merged
merged 4 commits into from
Sep 25, 2023
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
2 changes: 2 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/MorphToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ public function newPivot(array $attributes = [], $exists = false)
{
$using = $this->using;

$attributes = array_merge([$this->morphType => $this->morphClass], $attributes);

$pivot = $using ? $using::fromRawAttributes($this->parent, $attributes, $this->table, $exists)
: MorphPivot::fromAttributes($this->parent, $attributes, $this->table, $exists);

Expand Down
67 changes: 67 additions & 0 deletions tests/Integration/Database/EloquentPivotEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Tests\Integration\Database;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphPivot;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Expand Down Expand Up @@ -31,6 +32,18 @@ protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
$table->timestamps();
});

Schema::create('equipments', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});

Schema::create('equipmentables', function (Blueprint $table) {
$table->increments('id');
$table->morphs('equipmentable');
$table->foreignId('equipment_id');
});

Schema::create('project_users', function (Blueprint $table) {
$table->integer('user_id');
$table->integer('project_id');
Expand Down Expand Up @@ -120,13 +133,47 @@ public function testCustomPivotUpdateEventHasDirtyCorrect()

$this->assertSame(['role' => 'Lead Developer'], $_SERVER['pivot_dirty_attributes']);
}

public function testCustomMorphPivotClassDetachAttributes()
{
$project = PivotEventsTestProject::forceCreate([
'name' => 'Test Project',
]);

PivotEventsTestModelEquipment::deleting(function ($model) use ($project) {
$this->assertInstanceOf(PivotEventsTestProject::class, $model->equipmentable);
$this->assertEquals($project->id, $model->equipmentable->id);
});

$equipment = PivotEventsTestEquipment::forceCreate([
'name' => 'important-equipment',
]);

$project->equipments()->save($equipment);
$equipment->projects()->sync([]);
}
}

class PivotEventsTestUser extends Model
{
public $table = 'users';
}

class PivotEventsTestEquipment extends Model
{
public $table = 'equipments';

public function getForeignKey()
{
return 'equipment_id';
}

public function projects()
{
return $this->morphedByMany(PivotEventsTestProject::class, 'equipmentable')->using(PivotEventsTestModelEquipment::class);
}
}

class PivotEventsTestProject extends Model
{
public $table = 'projects';
Expand All @@ -144,6 +191,26 @@ public function contributors()
->using(PivotEventsTestCollaborator::class)
->wherePivot('role', 'contributor');
}

public function equipments()
{
return $this->morphToMany(PivotEventsTestEquipment::class, 'equipmentable')->using(PivotEventsTestModelEquipment::class);
}
}

class PivotEventsTestModelEquipment extends MorphPivot
{
public $table = 'equipmentables';

public function equipment()
{
return $this->belongsTo(PivotEventsTestEquipment::class);
}

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

class PivotEventsTestCollaborator extends Pivot
Expand Down