Skip to content

Commit 098347c

Browse files
authored
Merge pull request #2498 from jenssegers/pr_2318
fix: morphTo when relation name is in camel case
2 parents 551ec9f + bd35157 commit 098347c

File tree

5 files changed

+11
-11
lines changed

5 files changed

+11
-11
lines changed

Diff for: src/Eloquent/HybridRelations.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,10 @@ public function morphTo($name = null, $type = null, $id = null, $ownerKey = null
180180
if ($name === null) {
181181
[$current, $caller] = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
182182

183-
$name = Str::snake($caller['function']);
183+
$name = $caller['function'];
184184
}
185185

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

188188
// If the type value is null it is probably safe to assume we're eager loading
189189
// the relationship. When that is the case we will pass in a dummy query as

Diff for: tests/RelationsTest.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -370,21 +370,21 @@ public function testMorph(): void
370370
$this->assertEquals($photo->id, $client->photo->id);
371371

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

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

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

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

390390
public function testHasManyHas(): void

Diff for: tests/models/Client.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function users(): BelongsToMany
2020

2121
public function photo(): MorphOne
2222
{
23-
return $this->morphOne('Photo', 'imageable');
23+
return $this->morphOne('Photo', 'has_image');
2424
}
2525

2626
public function addresses(): HasMany

Diff for: tests/models/Photo.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Photo extends Eloquent
1111
protected $collection = 'photos';
1212
protected static $unguarded = true;
1313

14-
public function imageable(): MorphTo
14+
public function hasImage(): MorphTo
1515
{
1616
return $this->morphTo();
1717
}

Diff for: tests/models/User.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function groups()
7373

7474
public function photos()
7575
{
76-
return $this->morphMany('Photo', 'imageable');
76+
return $this->morphMany('Photo', 'has_image');
7777
}
7878

7979
public function addresses()

0 commit comments

Comments
 (0)