Skip to content

Commit a6b1839

Browse files
authored
Merge pull request #4402 from Laravel-Backpack/fix-infering-the-connecting-key-on-HasMany
fix infering the connecting key on HasMany relations
2 parents d8aa4d0 + 8c375c9 commit a6b1839

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -295,22 +295,22 @@ private function createManyEntries($entry, $relation, $relationMethod, $relation
295295
{
296296
$items = $relationDetails['values'][$relationMethod];
297297

298-
$relation_local_key = $relation->getLocalKeyName();
298+
$relatedModelLocalKey = $relation->getRelated()->getKeyName();
299299

300300
$relatedItemsSent = [];
301301

302302
foreach ($items as $item) {
303303
[$directInputs, $relationInputs] = $this->splitInputIntoDirectAndRelations($item, $relationDetails, $relationMethod);
304304
// for each item we get the inputs to create and the relations of it.
305-
$relation_local_key_value = $item[$relation_local_key] ?? null;
305+
$relatedModelLocalKeyValue = $item[$relatedModelLocalKey] ?? null;
306306

307307
// we either find the matched entry by local_key (usually `id`)
308308
// and update the values from the input
309309
// or create a new item from input
310-
$item = $entry->{$relationMethod}()->updateOrCreate([$relation_local_key => $relation_local_key_value], $directInputs);
310+
$item = $entry->{$relationMethod}()->updateOrCreate([$relatedModelLocalKey => $relatedModelLocalKeyValue], $directInputs);
311311

312312
// we store the item local key so we can match them with database and check if any item was deleted
313-
$relatedItemsSent[] = $item->{$relation_local_key};
313+
$relatedItemsSent[] = $item->{$relatedModelLocalKey};
314314

315315
// create the item relations if any.
316316
$this->createRelationsForItem($item, $relationInputs);
@@ -319,7 +319,7 @@ private function createManyEntries($entry, $relation, $relationMethod, $relation
319319
// use the collection of sent ids to match agains database ids, delete the ones not found in the submitted ids.
320320
if (! empty($relatedItemsSent)) {
321321
// we perform the cleanup of removed database items
322-
$entry->{$relationMethod}()->whereNotIn($relation_local_key, $relatedItemsSent)->delete();
322+
$entry->{$relationMethod}()->whereNotIn($relatedModelLocalKey, $relatedItemsSent)->delete();
323323
}
324324
}
325325
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ protected function makeSureSubfieldsHaveNecessaryAttributes($field)
302302
$entity = isset($field['baseEntity']) ? $field['baseEntity'].'.'.$field['entity'] : $field['entity'];
303303
$relationInstance = $this->getRelationInstance(['entity' => $entity]);
304304
$field['subfields'] = Arr::prepend($field['subfields'], [
305-
'name' => $relationInstance->getLocalKeyName(),
305+
'name' => $relationInstance->getRelated()->getKeyName(),
306306
'type' => 'hidden',
307307
]);
308308
break;

0 commit comments

Comments
 (0)