Skip to content

fix: morphTo when relation name is in camel case #2318

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 3 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
74 changes: 41 additions & 33 deletions src/Eloquent/HybridRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ trait HybridRelations
{
/**
* Define a one-to-one relationship.
* @param string $related
* @param string $foreignKey
* @param string $localKey
*
* @param string $related
* @param string $foreignKey
* @param string $localKey
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function hasOne($related, $foreignKey = null, $localKey = null)
Expand All @@ -39,11 +40,12 @@ public function hasOne($related, $foreignKey = null, $localKey = null)

/**
* Define a polymorphic one-to-one relationship.
* @param string $related
* @param string $name
* @param string $type
* @param string $id
* @param string $localKey
*
* @param string $related
* @param string $name
* @param string $type
* @param string $id
* @param string $localKey
* @return \Illuminate\Database\Eloquent\Relations\MorphOne
*/
public function morphOne($related, $name, $type = null, $id = null, $localKey = null)
Expand All @@ -64,9 +66,10 @@ public function morphOne($related, $name, $type = null, $id = null, $localKey =

/**
* Define a one-to-many relationship.
* @param string $related
* @param string $foreignKey
* @param string $localKey
*
* @param string $related
* @param string $foreignKey
* @param string $localKey
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function hasMany($related, $foreignKey = null, $localKey = null)
Expand All @@ -87,11 +90,12 @@ public function hasMany($related, $foreignKey = null, $localKey = null)

/**
* Define a polymorphic one-to-many relationship.
* @param string $related
* @param string $name
* @param string $type
* @param string $id
* @param string $localKey
*
* @param string $related
* @param string $name
* @param string $type
* @param string $id
* @param string $localKey
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function morphMany($related, $name, $type = null, $id = null, $localKey = null)
Expand All @@ -117,10 +121,11 @@ public function morphMany($related, $name, $type = null, $id = null, $localKey =

/**
* Define an inverse one-to-one or many relationship.
* @param string $related
* @param string $foreignKey
* @param string $otherKey
* @param string $relation
*
* @param string $related
* @param string $foreignKey
* @param string $otherKey
* @param string $relation
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null)
Expand Down Expand Up @@ -160,10 +165,11 @@ public function belongsTo($related, $foreignKey = null, $otherKey = null, $relat

/**
* Define a polymorphic, inverse one-to-one or many relationship.
* @param string $name
* @param string $type
* @param string $id
* @param string $ownerKey
*
* @param string $name
* @param string $type
* @param string $id
* @param string $ownerKey
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function morphTo($name = null, $type = null, $id = null, $ownerKey = null)
Expand All @@ -174,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 All @@ -204,13 +210,14 @@ public function morphTo($name = null, $type = null, $id = null, $ownerKey = null

/**
* Define a many-to-many relationship.
* @param string $related
* @param string $collection
* @param string $foreignKey
* @param string $otherKey
* @param string $parentKey
* @param string $relatedKey
* @param string $relation
*
* @param string $related
* @param string $collection
* @param string $foreignKey
* @param string $otherKey
* @param string $parentKey
* @param string $relatedKey
* @param string $relation
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function belongsToMany(
Expand Down Expand Up @@ -277,6 +284,7 @@ public function belongsToMany(

/**
* Get the relationship name of the belongs to many.
*
* @return string
*/
protected function guessBelongsToManyRelation()
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
3 changes: 2 additions & 1 deletion tests/models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

/**
* Class User.
*
* @property string $_id
* @property string $name
* @property string $email
Expand Down Expand Up @@ -66,7 +67,7 @@ public function groups()

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

public function addresses()
Expand Down