Skip to content

Commit

Permalink
Merge pull request #2498 from jenssegers/pr_2318
Browse files Browse the repository at this point in the history
fix: morphTo when relation name is in camel case
  • Loading branch information
Smolevich authored Jan 16, 2023
2 parents 551ec9f + bd35157 commit 098347c
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/Eloquent/HybridRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,10 @@ public function morphTo($name = null, $type = null, $id = null, $ownerKey = null
if ($name === null) {
[$current, $caller] = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);

$name = Str::snake($caller['function']);
$name = $caller['function'];
}

[$type, $id] = $this->getMorphs($name, $type, $id);
[$type, $id] = $this->getMorphs(Str::snake($name), $type, $id);

// If the type value is null it is probably safe to assume we're eager loading
// the relationship. When that is the case we will pass in a dummy query as
Expand Down
12 changes: 6 additions & 6 deletions tests/RelationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,21 +370,21 @@ public function testMorph(): void
$this->assertEquals($photo->id, $client->photo->id);

$photo = Photo::first();
$this->assertEquals($photo->imageable->name, $user->name);
$this->assertEquals($photo->hasImage->name, $user->name);

$user = User::with('photos')->find($user->_id);
$relations = $user->getRelations();
$this->assertArrayHasKey('photos', $relations);
$this->assertEquals(1, $relations['photos']->count());

$photos = Photo::with('imageable')->get();
$photos = Photo::with('hasImage')->get();
$relations = $photos[0]->getRelations();
$this->assertArrayHasKey('imageable', $relations);
$this->assertInstanceOf(User::class, $photos[0]->imageable);
$this->assertArrayHasKey('hasImage', $relations);
$this->assertInstanceOf(User::class, $photos[0]->hasImage);

$relations = $photos[1]->getRelations();
$this->assertArrayHasKey('imageable', $relations);
$this->assertInstanceOf(Client::class, $photos[1]->imageable);
$this->assertArrayHasKey('hasImage', $relations);
$this->assertInstanceOf(Client::class, $photos[1]->hasImage);
}

public function testHasManyHas(): void
Expand Down
2 changes: 1 addition & 1 deletion tests/models/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function users(): BelongsToMany

public function photo(): MorphOne
{
return $this->morphOne('Photo', 'imageable');
return $this->morphOne('Photo', 'has_image');
}

public function addresses(): HasMany
Expand Down
2 changes: 1 addition & 1 deletion tests/models/Photo.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Photo extends Eloquent
protected $collection = 'photos';
protected static $unguarded = true;

public function imageable(): MorphTo
public function hasImage(): MorphTo
{
return $this->morphTo();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function groups()

public function photos()
{
return $this->morphMany('Photo', 'imageable');
return $this->morphMany('Photo', 'has_image');
}

public function addresses()
Expand Down

0 comments on commit 098347c

Please sign in to comment.