Skip to content

Commit

Permalink
Merge pull request #602 from Jhnbrn90/feature/snake-case-relationships
Browse files Browse the repository at this point in the history
Feature/snake case relationships
  • Loading branch information
Gummibeer authored Oct 6, 2019
2 parents f280a8a + 24bcf06 commit a63f6c3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Traits/DetectsChanges.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ protected static function getRelatedModelAttributeValue(Model $model, string $at

[$relatedModelName, $relatedAttribute] = explode('.', $attribute);

$relatedModelName = Str::camel($relatedModelName);

$relatedModel = $model->$relatedModelName ?? $model->$relatedModelName();

return ["{$relatedModelName}.{$relatedAttribute}" => $relatedModel->$relatedAttribute ?? null];
Expand Down
46 changes: 46 additions & 0 deletions tests/DetectsChangesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,52 @@ public function it_can_store_the_changes_when_updating_a_related_model()
$this->assertEquals($expectedChanges, $this->getLastActivity()->changes()->toArray());
}

/** @test */
public function it_can_store_the_changes_when_updating_a_snake_case_related_model()
{
$articleClass = new class() extends Article {
public static $logAttributes = ['name', 'text', 'snake_user.name'];

use LogsActivity;

public function snakeUser()
{
return $this->belongsTo(User::class, 'user_id');
}
};

$user = User::create([
'name' => 'a name',
]);

$anotherUser = User::create([
'name' => 'another name',
]);

$article = $articleClass::create([
'name' => 'name',
'text' => 'text',
'user_id' => $user->id,
]);

$article->user()->associate($anotherUser)->save();

$expectedChanges = [
'attributes' => [
'name' => 'name',
'text' => 'text',
'snakeUser.name' => 'another name',
],
'old' => [
'name' => 'name',
'text' => 'text',
'snakeUser.name' => 'a name',
],
];

$this->assertEquals($expectedChanges, $this->getLastActivity()->changes()->toArray());
}

/** @test */
public function it_can_store_the_dirty_changes_when_updating_a_related_model()
{
Expand Down

0 comments on commit a63f6c3

Please sign in to comment.