Skip to content

Commit 805cbb8

Browse files
authored
Merge pull request #4170 from Laravel-Backpack/refresh-the-relation-upon-item-creation
[v5] use a fresh relation instance to create related items
2 parents a118a41 + 030c66a commit 805cbb8

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

src/app/Library/CrudPanel/Traits/Create.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,12 @@ private function createManyEntries($entry, $relation, $relationMethod, $relation
298298
// we either find the matched entry by local_key (usually `id`)
299299
// and update the values from the input
300300
// or create a new item from input
301-
$item = $relation->updateOrCreate([$relation_local_key => $relation_local_key_value], $directInputs);
301+
$item = $entry->{$relationMethod}()->updateOrCreate([$relation_local_key => $relation_local_key_value], $directInputs);
302302

303-
// we store the item local key do we can match them with database and check if any item was deleted
303+
// we store the item local key so we can match them with database and check if any item was deleted
304304
$relatedItemsSent[] = $item->{$relation_local_key};
305305

306-
// create the item relations if any
306+
// create the item relations if any.
307307
$this->createRelationsForItem($item, $relationInputs);
308308
}
309309

tests/Unit/CrudPanel/CrudPanelCreateTest.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,7 @@ public function testHasManyCreatableRelationship()
855855
'remember_token' => null,
856856
'universes' => [
857857
[
858+
'id' => null,
858859
'title' => 'this is the star 1 title',
859860
],
860861
[
@@ -872,17 +873,45 @@ public function testHasManyCreatableRelationship()
872873
$inputData['universes'] = [
873874
[
874875
'id' => 1,
875-
'title' => 'only one star with changed title',
876+
'title' => 'star 1 with changed title',
877+
],
878+
[
879+
'id' => 2,
880+
'title' => 'star 2 with changed title',
876881
],
877882
];
878883

879884
$this->crudPanel->update($entry->id, $inputData);
880885

881-
$this->assertCount(1, $entry->fresh()->universes);
886+
$universes = $entry->fresh()->universes;
887+
$this->assertCount(2, $universes);
888+
$this->assertEquals([1, 2], $universes->pluck('id')->toArray());
889+
890+
$inputData['universes'] = [
891+
[
892+
'id' => 1,
893+
'title' => 'only one star with changed title',
894+
],
895+
];
896+
897+
$this->crudPanel->update($entry->id, $inputData);
882898

883899
$this->assertEquals($inputData['universes'][0]['title'], $entry->fresh()->universes->first()->title);
884900
$this->assertEquals($inputData['universes'][0]['id'], $entry->fresh()->universes->first()->id);
885901
$this->assertEquals(1, Universe::all()->count());
902+
903+
$inputData['universes'] = [
904+
[
905+
'id' => null,
906+
'title' => 'new star 3',
907+
],
908+
];
909+
910+
$this->crudPanel->update($entry->id, $inputData);
911+
912+
$this->assertEquals($inputData['universes'][0]['title'], $entry->fresh()->universes->first()->title);
913+
$this->assertEquals(3, $entry->fresh()->universes->first()->id);
914+
$this->assertEquals(1, Universe::all()->count());
886915
}
887916

888917
public function testHasManySelectableRelationshipWithoutForceDelete()

0 commit comments

Comments
 (0)