From 5fc67b14f02d65a1f58e0f83674e76b37b2ab266 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sun, 14 Jan 2018 09:14:19 -0600 Subject: [PATCH 1/8] write failing test --- .../EloquentPivotSerializationTest.php | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 tests/Integration/Database/EloquentPivotSerializationTest.php diff --git a/tests/Integration/Database/EloquentPivotSerializationTest.php b/tests/Integration/Database/EloquentPivotSerializationTest.php new file mode 100644 index 000000000000..40d084b4d4d3 --- /dev/null +++ b/tests/Integration/Database/EloquentPivotSerializationTest.php @@ -0,0 +1,90 @@ +increments('id'); + $table->string('email'); + $table->timestamps(); + }); + + Schema::create('projects', function ($table) { + $table->increments('id'); + $table->string('name'); + $table->timestamps(); + }); + + Schema::create('project_users', function ($table) { + $table->integer('user_id'); + $table->integer('project_id'); + }); + } + + + public function test_pivot_can_be_serialized_and_restored() + { + $user = PivotSerializationTestUser::forceCreate(['email' => 'taylor@laravel.com']); + $project = PivotSerializationTestProject::forceCreate(['name' => 'Test Project']); + $project->collaborators()->attach($user); + + $project = $project->fresh(); + + $class = new PivotSerializationTestClass($project->collaborators->first()->pivot); + $class = unserialize(serialize($class)); + + $this->assertEquals($project->collaborators->first()->pivot->user_id, $class->collaborator->user_id); + $this->assertEquals($project->collaborators->first()->pivot->project_id, $class->collaborator->project_id); + } +} + + +class PivotSerializationTestClass +{ + use SerializesModels; + + public $collaborator; + + public function __construct($collaborator) + { + $this->collaborator = $collaborator; + } +} + + +class PivotSerializationTestUser extends Model +{ + public $table = 'users'; +} + + +class PivotSerializationTestProject extends Model +{ + public $table = 'projects'; + + public function collaborators() + { + return $this->belongsToMany( + PivotSerializationTestUser::class, 'project_users', 'project_id', 'user_id' + )->using(PivotSerializationTestCollaborator::class); + } +} + + +class PivotSerializationTestCollaborator extends Pivot +{ + public $table = 'project_users'; +} From 77db0494d4a19782a71a2e649e4e0d6f86f8f3b8 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sun, 14 Jan 2018 09:18:01 -0600 Subject: [PATCH 2/8] write failing test for collections --- .../EloquentPivotSerializationTest.php | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/Integration/Database/EloquentPivotSerializationTest.php b/tests/Integration/Database/EloquentPivotSerializationTest.php index 40d084b4d4d3..af8bbdaa1c5a 100644 --- a/tests/Integration/Database/EloquentPivotSerializationTest.php +++ b/tests/Integration/Database/EloquentPivotSerializationTest.php @@ -49,6 +49,25 @@ public function test_pivot_can_be_serialized_and_restored() $this->assertEquals($project->collaborators->first()->pivot->user_id, $class->collaborator->user_id); $this->assertEquals($project->collaborators->first()->pivot->project_id, $class->collaborator->project_id); } + + + public function test_collection_of_pivots_can_be_serialized_and_restored() + { + $user = PivotSerializationTestUser::forceCreate(['email' => 'taylor@laravel.com']); + $user2 = PivotSerializationTestUser::forceCreate(['email' => 'mohamed@laravel.com']); + $project = PivotSerializationTestProject::forceCreate(['name' => 'Test Project']); + + $project->collaborators()->attach($user); + $project->collaborators()->attach($user2); + + $project = $project->fresh(); + + $class = new PivotSerializationTestCollectionClass($project->collaborators->map->pivot); + $class = unserialize(serialize($class)); + + $this->assertEquals($project->collaborators[0]->pivot->user_id, $class->collaborators[0]->user_id); + $this->assertEquals($project->collaborators[1]->pivot->project_id, $class->collaborators[1]->project_id); + } } @@ -65,6 +84,19 @@ public function __construct($collaborator) } +class PivotSerializationTestCollectionClass +{ + use SerializesModels; + + public $collaborators; + + public function __construct($collaborators) + { + $this->collaborators = $collaborators; + } +} + + class PivotSerializationTestUser extends Model { public $table = 'users'; From e5eef73e8e3ced78430eb3a1940fed37c23aad4a Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sun, 14 Jan 2018 09:38:06 -0600 Subject: [PATCH 3/8] fix pivot serialization --- .../Database/Eloquent/Collection.php | 9 +++ .../Database/Eloquent/Relations/Pivot.php | 67 +++++++++++++++++++ .../EloquentPivotSerializationTest.php | 6 +- 3 files changed, 81 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/Collection.php b/src/Illuminate/Database/Eloquent/Collection.php index 26d62b273fee..c7fd09e7b4b1 100755 --- a/src/Illuminate/Database/Eloquent/Collection.php +++ b/src/Illuminate/Database/Eloquent/Collection.php @@ -4,6 +4,7 @@ use LogicException; use Illuminate\Support\Arr; +use Illuminate\Database\Eloquent\Relations\Pivot; use Illuminate\Contracts\Queue\QueueableCollection; use Illuminate\Support\Collection as BaseCollection; @@ -407,6 +408,14 @@ public function getQueueableClass() */ public function getQueueableIds() { + if ($this->isEmpty()) { + return []; + } + + if ($this->first() instanceof Pivot) { + return $this->map->getQueueableId()->all(); + } + return $this->modelKeys(); } diff --git a/src/Illuminate/Database/Eloquent/Relations/Pivot.php b/src/Illuminate/Database/Eloquent/Relations/Pivot.php index e80aa911d567..c25bb011f3b9 100755 --- a/src/Illuminate/Database/Eloquent/Relations/Pivot.php +++ b/src/Illuminate/Database/Eloquent/Relations/Pivot.php @@ -222,4 +222,71 @@ public function getUpdatedAtColumn() { return $this->pivotParent->getUpdatedAtColumn(); } + + /** + * Get the queueable identity for the entity. + * + * @return mixed + */ + public function getQueueableId() + { + if (isset($this->attributes[$this->getKeyName()])) { + return $this->getKey(); + } + + return sprintf( + '%s:%s:%s:%s', + $this->foreignKey, $this->getAttribute($this->foreignKey), + $this->relatedKey, $this->getAttribute($this->relatedKey) + ); + } + + /** + * Get a new query to restore one or more models by their queueable IDs. + * + * @param array|int $ids + * @return \Illuminate\Database\Eloquent\Builder + */ + public function newQueryForRestoration($ids) + { + if (is_array($ids)) { + return $this->newQueryForCollectionRestoration($ids); + } + + if (! Str::contains($ids, ':')) { + return parent::newQueryForRestoration($ids); + } + + $segments = explode(':', $ids); + + return $this->newQueryWithoutScopes() + ->where($segments[0], $segments[1]) + ->where($segments[2], $segments[3]); + } + + /** + * Get a new query to restore multiple models by their queueable IDs. + * + * @param array|int $ids + * @return \Illuminate\Database\Eloquent\Builder + */ + protected function newQueryForCollectionRestoration(array $ids) + { + if (! Str::contains($ids[0], ':')) { + return parent::newQueryForRestoration($ids); + } + + $query = $this->newQueryWithoutScopes(); + + foreach ($ids as $id) { + $segments = explode(':', $id); + + $query->orWhere(function ($query) use ($segments) { + return $query->where($segments[0], $segments[1]) + ->where($segments[2], $segments[3]); + }); + } + + return $query; + } } diff --git a/tests/Integration/Database/EloquentPivotSerializationTest.php b/tests/Integration/Database/EloquentPivotSerializationTest.php index af8bbdaa1c5a..019d891a46b5 100644 --- a/tests/Integration/Database/EloquentPivotSerializationTest.php +++ b/tests/Integration/Database/EloquentPivotSerializationTest.php @@ -6,6 +6,7 @@ use Illuminate\Support\Facades\Schema; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\Pivot; +use Illuminate\Database\Eloquent\Collection as DatabaseCollection; /** * @group integration @@ -51,6 +52,9 @@ public function test_pivot_can_be_serialized_and_restored() } + /** + * @group wow + */ public function test_collection_of_pivots_can_be_serialized_and_restored() { $user = PivotSerializationTestUser::forceCreate(['email' => 'taylor@laravel.com']); @@ -62,7 +66,7 @@ public function test_collection_of_pivots_can_be_serialized_and_restored() $project = $project->fresh(); - $class = new PivotSerializationTestCollectionClass($project->collaborators->map->pivot); + $class = new PivotSerializationTestCollectionClass(DatabaseCollection::make($project->collaborators->map->pivot)); $class = unserialize(serialize($class)); $this->assertEquals($project->collaborators[0]->pivot->user_id, $class->collaborators[0]->user_id); From bbc9415d82d71a2901474afb845dd5dee2d46ca5 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sun, 14 Jan 2018 09:38:44 -0600 Subject: [PATCH 4/8] formatting --- src/Illuminate/Database/Eloquent/Collection.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Collection.php b/src/Illuminate/Database/Eloquent/Collection.php index c7fd09e7b4b1..c9fd5868e9fa 100755 --- a/src/Illuminate/Database/Eloquent/Collection.php +++ b/src/Illuminate/Database/Eloquent/Collection.php @@ -412,11 +412,9 @@ public function getQueueableIds() return []; } - if ($this->first() instanceof Pivot) { - return $this->map->getQueueableId()->all(); - } - - return $this->modelKeys(); + return $this->first() instanceof Pivot + ? $this->map->getQueueableId()->all() + : $this->modelKeys(); } /** From 1b5a6429f95a35e50c4ec503870af8f69d862ba0 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sun, 14 Jan 2018 09:39:36 -0600 Subject: [PATCH 5/8] get rid of group --- tests/Integration/Database/EloquentPivotSerializationTest.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/Integration/Database/EloquentPivotSerializationTest.php b/tests/Integration/Database/EloquentPivotSerializationTest.php index 019d891a46b5..7b18c89ce58e 100644 --- a/tests/Integration/Database/EloquentPivotSerializationTest.php +++ b/tests/Integration/Database/EloquentPivotSerializationTest.php @@ -52,9 +52,6 @@ public function test_pivot_can_be_serialized_and_restored() } - /** - * @group wow - */ public function test_collection_of_pivots_can_be_serialized_and_restored() { $user = PivotSerializationTestUser::forceCreate(['email' => 'taylor@laravel.com']); From 6c6a7044c7fbc67ce57e8e86b2b322ad483429e3 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sun, 14 Jan 2018 10:10:39 -0600 Subject: [PATCH 6/8] fix morph pivot serialization and restoration --- .../Eloquent/Relations/MorphPivot.php | 71 ++++++++++++++ .../Eloquent/Relations/MorphToMany.php | 16 +++ .../EloquentPivotSerializationTest.php | 97 +++++++++++++++++-- 3 files changed, 174 insertions(+), 10 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Relations/MorphPivot.php b/src/Illuminate/Database/Eloquent/Relations/MorphPivot.php index b7a2f34195b8..a8a9210f0e45 100644 --- a/src/Illuminate/Database/Eloquent/Relations/MorphPivot.php +++ b/src/Illuminate/Database/Eloquent/Relations/MorphPivot.php @@ -2,6 +2,7 @@ namespace Illuminate\Database\Eloquent\Relations; +use Illuminate\Support\Str; use Illuminate\Database\Eloquent\Builder; class MorphPivot extends Pivot @@ -76,4 +77,74 @@ public function setMorphClass($morphClass) return $this; } + + /** + * Get the queueable identity for the entity. + * + * @return mixed + */ + public function getQueueableId() + { + if (isset($this->attributes[$this->getKeyName()])) { + return $this->getKey(); + } + + return sprintf( + '%s:%s:%s:%s:%s:%s', + $this->foreignKey, $this->getAttribute($this->foreignKey), + $this->relatedKey, $this->getAttribute($this->relatedKey), + $this->morphType, $this->morphClass + ); + } + + /** + * Get a new query to restore one or more models by their queueable IDs. + * + * @param array|int $ids + * @return \Illuminate\Database\Eloquent\Builder + */ + public function newQueryForRestoration($ids) + { + if (is_array($ids)) { + return $this->newQueryForCollectionRestoration($ids); + } + + if (! Str::contains($ids, ':')) { + return parent::newQueryForRestoration($ids); + } + + $segments = explode(':', $ids); + + return $this->newQueryWithoutScopes() + ->where($segments[0], $segments[1]) + ->where($segments[2], $segments[3]) + ->where($segments[4], $segments[5]); + } + + /** + * Get a new query to restore multiple models by their queueable IDs. + * + * @param array|int $ids + * @return \Illuminate\Database\Eloquent\Builder + */ + protected function newQueryForCollectionRestoration(array $ids) + { + if (! Str::contains($ids[0], ':')) { + return parent::newQueryForRestoration($ids); + } + + $query = $this->newQueryWithoutScopes(); + + foreach ($ids as $id) { + $segments = explode(':', $id); + + $query->orWhere(function ($query) use ($segments) { + return $query->where($segments[0], $segments[1]) + ->where($segments[2], $segments[3]) + ->where($segments[4], $segments[5]); + }); + } + + return $query; + } } diff --git a/src/Illuminate/Database/Eloquent/Relations/MorphToMany.php b/src/Illuminate/Database/Eloquent/Relations/MorphToMany.php index 15f5cfd028c4..da6658513af9 100644 --- a/src/Illuminate/Database/Eloquent/Relations/MorphToMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/MorphToMany.php @@ -146,6 +146,22 @@ public function newPivot(array $attributes = [], $exists = false) return $pivot; } + /** + * Get the pivot columns for the relation. + * + * "pivot_" is prefixed ot each column for easy removal later. + * + * @return array + */ + protected function aliasedPivotColumns() + { + $defaults = [$this->foreignPivotKey, $this->relatedPivotKey, $this->morphType]; + + return collect(array_merge($defaults, $this->pivotColumns))->map(function ($column) { + return $this->table.'.'.$column.' as pivot_'.$column; + })->unique()->all(); + } + /** * Get the foreign key "type" name. * diff --git a/tests/Integration/Database/EloquentPivotSerializationTest.php b/tests/Integration/Database/EloquentPivotSerializationTest.php index 7b18c89ce58e..8dc45a300c12 100644 --- a/tests/Integration/Database/EloquentPivotSerializationTest.php +++ b/tests/Integration/Database/EloquentPivotSerializationTest.php @@ -6,6 +6,7 @@ use Illuminate\Support\Facades\Schema; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\Pivot; +use Illuminate\Database\Eloquent\Relations\MorphPivot; use Illuminate\Database\Eloquent\Collection as DatabaseCollection; /** @@ -33,6 +34,18 @@ public function setUp() $table->integer('user_id'); $table->integer('project_id'); }); + + Schema::create('tags', function ($table) { + $table->increments('id'); + $table->string('name'); + $table->timestamps(); + }); + + Schema::create('taggables', function ($table) { + $table->integer('tag_id'); + $table->integer('taggable_id'); + $table->string('taggable_type'); + }); } @@ -47,8 +60,25 @@ public function test_pivot_can_be_serialized_and_restored() $class = new PivotSerializationTestClass($project->collaborators->first()->pivot); $class = unserialize(serialize($class)); - $this->assertEquals($project->collaborators->first()->pivot->user_id, $class->collaborator->user_id); - $this->assertEquals($project->collaborators->first()->pivot->project_id, $class->collaborator->project_id); + $this->assertEquals($project->collaborators->first()->pivot->user_id, $class->pivot->user_id); + $this->assertEquals($project->collaborators->first()->pivot->project_id, $class->pivot->project_id); + } + + + public function test_morph_pivot_can_be_serialized_and_restored() + { + $project = PivotSerializationTestProject::forceCreate(['name' => 'Test Project']); + $tag = PivotSerializationTestTag::forceCreate(['name' => 'Test Tag']); + $project->tags()->attach($tag); + + $project = $project->fresh(); + + $class = new PivotSerializationTestClass($project->tags->first()->pivot); + $class = unserialize(serialize($class)); + + $this->assertEquals($project->tags->first()->pivot->tag_id, $class->pivot->tag_id); + $this->assertEquals($project->tags->first()->pivot->taggable_id, $class->pivot->taggable_id); + $this->assertEquals($project->tags->first()->pivot->taggable_type, $class->pivot->taggable_type); } @@ -66,8 +96,32 @@ public function test_collection_of_pivots_can_be_serialized_and_restored() $class = new PivotSerializationTestCollectionClass(DatabaseCollection::make($project->collaborators->map->pivot)); $class = unserialize(serialize($class)); - $this->assertEquals($project->collaborators[0]->pivot->user_id, $class->collaborators[0]->user_id); - $this->assertEquals($project->collaborators[1]->pivot->project_id, $class->collaborators[1]->project_id); + $this->assertEquals($project->collaborators[0]->pivot->user_id, $class->pivots[0]->user_id); + $this->assertEquals($project->collaborators[1]->pivot->project_id, $class->pivots[1]->project_id); + } + + + public function test_collection_of_morph_pivots_can_be_serialized_and_restored() + { + $tag = PivotSerializationTestTag::forceCreate(['name' => 'Test Tag 1']); + $tag2 = PivotSerializationTestTag::forceCreate(['name' => 'Test Tag 2']); + $project = PivotSerializationTestProject::forceCreate(['name' => 'Test Project']); + + $project->tags()->attach($tag); + $project->tags()->attach($tag2); + + $project = $project->fresh(); + + $class = new PivotSerializationTestCollectionClass(DatabaseCollection::make($project->tags->map->pivot)); + $class = unserialize(serialize($class)); + + $this->assertEquals($project->tags[0]->pivot->tag_id, $class->pivots[0]->tag_id); + $this->assertEquals($project->tags[0]->pivot->taggable_id, $class->pivots[0]->taggable_id); + $this->assertEquals($project->tags[0]->pivot->taggable_type, $class->pivots[0]->taggable_type); + + $this->assertEquals($project->tags[1]->pivot->tag_id, $class->pivots[1]->tag_id); + $this->assertEquals($project->tags[1]->pivot->taggable_id, $class->pivots[1]->taggable_id); + $this->assertEquals($project->tags[1]->pivot->taggable_type, $class->pivots[1]->taggable_type); } } @@ -76,11 +130,11 @@ class PivotSerializationTestClass { use SerializesModels; - public $collaborator; + public $pivot; - public function __construct($collaborator) + public function __construct($pivot) { - $this->collaborator = $collaborator; + $this->pivot = $pivot; } } @@ -89,11 +143,11 @@ class PivotSerializationTestCollectionClass { use SerializesModels; - public $collaborators; + public $pivots; - public function __construct($collaborators) + public function __construct($pivots) { - $this->collaborators = $collaborators; + $this->pivots = $pivots; } } @@ -114,6 +168,24 @@ public function collaborators() PivotSerializationTestUser::class, 'project_users', 'project_id', 'user_id' )->using(PivotSerializationTestCollaborator::class); } + + public function tags() + { + return $this->morphToMany(PivotSerializationTestTag::class, 'taggable', 'taggables', 'taggable_id', 'tag_id') + ->using(PivotSerializationTestTagAttachment::class); + } +} + + +class PivotSerializationTestTag extends Model +{ + public $table = 'tags'; + + public function projects() + { + return $this->morphedByMany(PivotSerializationTestProject::class, 'taggable', 'taggables', 'tag_id', 'taggable_id') + ->using(PivotSerializationTestTagAttachment::class); + } } @@ -121,3 +193,8 @@ class PivotSerializationTestCollaborator extends Pivot { public $table = 'project_users'; } + +class PivotSerializationTestTagAttachment extends MorphPivot +{ + public $table = 'taggables'; +} From 0706ec739e9d2774b33c88393fbb88791b6775c3 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sun, 14 Jan 2018 10:11:50 -0600 Subject: [PATCH 7/8] Apply fixes from StyleCI (#22785) --- .../Database/EloquentPivotSerializationTest.php | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/tests/Integration/Database/EloquentPivotSerializationTest.php b/tests/Integration/Database/EloquentPivotSerializationTest.php index 8dc45a300c12..aed95428b85d 100644 --- a/tests/Integration/Database/EloquentPivotSerializationTest.php +++ b/tests/Integration/Database/EloquentPivotSerializationTest.php @@ -48,7 +48,6 @@ public function setUp() }); } - public function test_pivot_can_be_serialized_and_restored() { $user = PivotSerializationTestUser::forceCreate(['email' => 'taylor@laravel.com']); @@ -64,7 +63,6 @@ public function test_pivot_can_be_serialized_and_restored() $this->assertEquals($project->collaborators->first()->pivot->project_id, $class->pivot->project_id); } - public function test_morph_pivot_can_be_serialized_and_restored() { $project = PivotSerializationTestProject::forceCreate(['name' => 'Test Project']); @@ -81,7 +79,6 @@ public function test_morph_pivot_can_be_serialized_and_restored() $this->assertEquals($project->tags->first()->pivot->taggable_type, $class->pivot->taggable_type); } - public function test_collection_of_pivots_can_be_serialized_and_restored() { $user = PivotSerializationTestUser::forceCreate(['email' => 'taylor@laravel.com']); @@ -100,7 +97,6 @@ public function test_collection_of_pivots_can_be_serialized_and_restored() $this->assertEquals($project->collaborators[1]->pivot->project_id, $class->pivots[1]->project_id); } - public function test_collection_of_morph_pivots_can_be_serialized_and_restored() { $tag = PivotSerializationTestTag::forceCreate(['name' => 'Test Tag 1']); @@ -125,7 +121,6 @@ public function test_collection_of_morph_pivots_can_be_serialized_and_restored() } } - class PivotSerializationTestClass { use SerializesModels; @@ -138,7 +133,6 @@ public function __construct($pivot) } } - class PivotSerializationTestCollectionClass { use SerializesModels; @@ -151,13 +145,11 @@ public function __construct($pivots) } } - class PivotSerializationTestUser extends Model { public $table = 'users'; } - class PivotSerializationTestProject extends Model { public $table = 'projects'; @@ -176,7 +168,6 @@ public function tags() } } - class PivotSerializationTestTag extends Model { public $table = 'tags'; @@ -188,7 +179,6 @@ public function projects() } } - class PivotSerializationTestCollaborator extends Pivot { public $table = 'project_users'; From 91dbd2970dfcc4089e9740ea823b72eb9071a7e6 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sun, 14 Jan 2018 10:15:11 -0600 Subject: [PATCH 8/8] add to test --- tests/Integration/Database/EloquentPivotSerializationTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/Integration/Database/EloquentPivotSerializationTest.php b/tests/Integration/Database/EloquentPivotSerializationTest.php index 8dc45a300c12..9e21382d9a12 100644 --- a/tests/Integration/Database/EloquentPivotSerializationTest.php +++ b/tests/Integration/Database/EloquentPivotSerializationTest.php @@ -62,6 +62,8 @@ public function test_pivot_can_be_serialized_and_restored() $this->assertEquals($project->collaborators->first()->pivot->user_id, $class->pivot->user_id); $this->assertEquals($project->collaborators->first()->pivot->project_id, $class->pivot->project_id); + + $class->pivot->save(); } @@ -79,6 +81,8 @@ public function test_morph_pivot_can_be_serialized_and_restored() $this->assertEquals($project->tags->first()->pivot->tag_id, $class->pivot->tag_id); $this->assertEquals($project->tags->first()->pivot->taggable_id, $class->pivot->taggable_id); $this->assertEquals($project->tags->first()->pivot->taggable_type, $class->pivot->taggable_type); + + $class->pivot->save(); }