-
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.
[8.x] Fix pivot and morphpivot fresh and refresh methods (#35193)
* Fix pivot and morphpivot fresh and refresh methods * Add tests for refreshing of pivots
- Loading branch information
Showing
3 changed files
with
98 additions
and
3 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
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
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 |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Model as Eloquent; | ||
use Illuminate\Database\Eloquent\ModelNotFoundException; | ||
use Illuminate\Database\Eloquent\Relations\MorphPivot; | ||
use Illuminate\Database\Eloquent\Relations\Pivot; | ||
use Illuminate\Database\Eloquent\Relations\Relation; | ||
use Illuminate\Database\Eloquent\SoftDeletes; | ||
|
@@ -126,6 +127,18 @@ protected function createSchema() | |
$table->timestamps(); | ||
$table->softDeletes(); | ||
}); | ||
|
||
$this->schema($connection)->create('tags', function ($table) { | ||
$table->increments('id'); | ||
$table->string('name'); | ||
$table->timestamps(); | ||
}); | ||
|
||
$this->schema($connection)->create('taggables', function ($table) { | ||
$table->integer('tag_id'); | ||
$table->morphs('taggable'); | ||
$table->string('taxonomy')->nullable(); | ||
}); | ||
} | ||
|
||
$this->schema($connection)->create('non_incrementing_users', function ($table) { | ||
|
@@ -1742,6 +1755,53 @@ public function testChildModelsAreIgnored() | |
$this->assertFalse(Model::isIgnoringTouch()); | ||
} | ||
|
||
public function testPivotsCanBeRefreshed() | ||
{ | ||
EloquentTestFriendLevel::create(['id' => 1, 'level' => 'acquaintance']); | ||
EloquentTestFriendLevel::create(['id' => 2, 'level' => 'friend']); | ||
|
||
$user = EloquentTestUser::create(['id' => 1, 'email' => '[email protected]']); | ||
$user->friends()->create(['id' => 2, 'email' => '[email protected]'], ['friend_level_id' => 1]); | ||
|
||
$pivot = $user->friends[0]->pivot; | ||
|
||
// Simulate a change that happened externally | ||
DB::table('friends')->where('user_id', 1)->where('friend_id', 2)->update([ | ||
'friend_level_id' => 2, | ||
]); | ||
|
||
$this->assertInstanceOf(Pivot::class, $freshPivot = $pivot->fresh()); | ||
$this->assertEquals(2, $freshPivot->friend_level_id); | ||
|
||
$this->assertSame($pivot, $pivot->refresh()); | ||
$this->assertEquals(2, $pivot->friend_level_id); | ||
} | ||
|
||
public function testMorphPivotsCanBeRefreshed() | ||
{ | ||
$post = EloquentTestPost::create(['name' => 'MorphToMany Post', 'user_id' => 1]); | ||
$post->tags()->create(['id' => 1, 'name' => 'News']); | ||
|
||
$pivot = $post->tags[0]->pivot; | ||
|
||
// Simulate a change that happened externally | ||
DB::table('taggables') | ||
->where([ | ||
'taggable_type' => EloquentTestPost::class, | ||
'taggable_id' => 1, | ||
'tag_id' => 1, | ||
]) | ||
->update([ | ||
'taxonomy' => 'primary', | ||
]); | ||
|
||
$this->assertInstanceOf(MorphPivot::class, $freshPivot = $pivot->fresh()); | ||
$this->assertEquals('primary', $freshPivot->taxonomy); | ||
|
||
$this->assertSame($pivot, $pivot->refresh()); | ||
$this->assertEquals('primary', $pivot->taxonomy); | ||
} | ||
|
||
/** | ||
* Helpers... | ||
*/ | ||
|
@@ -1908,6 +1968,17 @@ public function parentPost() | |
{ | ||
return $this->belongsTo(self::class, 'parent_id'); | ||
} | ||
|
||
public function tags() | ||
{ | ||
return $this->morphToMany(EloquentTestTag::class, 'taggable', null, null, 'tag_id')->withPivot('taxonomy'); | ||
} | ||
} | ||
|
||
class EloquentTestTag extends Eloquent | ||
{ | ||
protected $table = 'tags'; | ||
protected $guarded = []; | ||
} | ||
|
||
class EloquentTestFriendLevel extends Eloquent | ||
|