Permalink
Browse files

Allow nullable relation (#308)

  • Loading branch information...
chelout authored and freekmurze committed Nov 13, 2017
1 parent 7079f46 commit 72ab83655ad140b11f8ae1fd1c3ca3ceb1e88ae8
Showing with 39 additions and 1 deletion.
  1. +1 −1 src/Traits/DetectsChanges.php
  2. +38 −0 tests/DetectsChangesTest.php
@@ -111,6 +111,6 @@ protected static function getRelatedModelAttributeValue(Model $model, string $at
$relatedModel = $model->$relatedModelName ?? $model->$relatedModelName();
return ["{$relatedModelName}.{$relatedAttribute}" => $relatedModel->$relatedAttribute];
return ["{$relatedModelName}.{$relatedAttribute}" => $relatedModel->$relatedAttribute ?? null];
}
}
@@ -81,6 +81,44 @@ public function it_can_store_the_relation_values_when_creating_a_model()
$this->assertEquals($expectedChanges, $this->getLastActivity()->changes()->toArray());
}
/** @test */
public function it_can_store_empty_relation_when_creating_a_model()
{
$articleClass = new class() extends Article {
public static $logAttributes = ['name', 'text', 'user.name'];
use LogsActivity;
};
$user = User::create([
'name' => 'user name',
]);
$article = $articleClass::create([
'name' => 'original name',
'text' => 'original text',
]);
$article->name = 'updated name';
$article->text = 'updated text';
$article->save();
$expectedChanges = [
'attributes' => [
'name' => 'updated name',
'text' => 'updated text',
'user.name' => null,
],
'old' => [
'name' => 'original name',
'text' => 'original text',
'user.name' => null,
],
];
$this->assertEquals($expectedChanges, $this->getLastActivity()->changes()->toArray());
}
/** @test */
public function it_can_store_the_changes_when_updating_a_model()
{

0 comments on commit 72ab836

Please sign in to comment.