Skip to content

Commit 0215a39

Browse files
authored
Merge pull request #3606 from Laravel-Backpack/relationship-columns-definition
Relationship columns definition
2 parents 589cf79 + f711ea0 commit 0215a39

File tree

5 files changed

+28
-5
lines changed

5 files changed

+28
-5
lines changed

src/app/Library/CrudPanel/CrudPanel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ public function getRelatedEntriesAttributes($model, $relationString, $attribute)
385385
//if attribute does not exist in main array we have more than one entry OR the attribute
386386
//is an acessor that is not in $appends property of model.
387387
if (! isset($entries[$attribute])) {
388-
//we first check if we don't have the attribute because it's and acessor that is not in appends.
388+
//we first check if we don't have the attribute because it's an acessor that is not in appends.
389389
if ($model_instance->hasGetMutator($attribute) && isset($entries[$modelKey])) {
390390
$entry_in_database = $model_instance->find($entries[$modelKey]);
391391
$attributes[$entry_in_database->{$modelKey}] = $this->parseTranslatableAttributes($model_instance, $attribute, $entry_in_database->{$attribute});
@@ -458,7 +458,7 @@ public function parseTranslatableAttributes($model, $attribute, $value)
458458
*/
459459
private function getRelatedEntries($model, $relationString)
460460
{
461-
$relationArray = explode('.', $relationString);
461+
$relationArray = explode('.', $this->getOnlyRelationEntity(['entity' => $relationString]));
462462
$firstRelationName = Arr::first($relationArray);
463463
$relation = $model->{$firstRelationName};
464464

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,10 +360,10 @@ public function firstColumnWhere($attribute, $value)
360360
public function makeSureColumnHasNeededAttributes($column)
361361
{
362362
$column = $this->makeSureColumnHasName($column);
363+
$column = $this->makeSureColumnHasKey($column);
363364
$column = $this->makeSureColumnHasLabel($column);
364365
$column = $this->makeSureColumnHasEntity($column);
365366
$column = $this->makeSureColumnHasType($column);
366-
$column = $this->makeSureColumnHasKey($column);
367367
$column = $this->makeSureColumnHasPriority($column);
368368
$column = $this->makeSureColumnHasModel($column);
369369

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,27 @@ protected function makeSureColumnHasEntity($column)
150150
// if it has parameters it's not a relation method.
151151
$column['entity'] = $this->modelMethodHasParameters($this->model, $possibleMethodName) ? false : $column['name'];
152152

153+
$parts = explode('.', $column['entity']);
154+
155+
$attribute_in_relation = false;
156+
157+
$model = $this->model;
158+
159+
// here we are going to iterate through all relation parts to check
160+
// if the attribute is present in the relation string.
161+
foreach ($parts as $i => $part) {
162+
try {
163+
$model = $model->$part()->getRelated();
164+
} catch (\Exception $e) {
165+
$attribute_in_relation = true;
166+
}
167+
}
168+
// if the user setup the attribute in relation string, we are not going to infer that attribute from model
169+
// instead we get the defined attribute by the user.
170+
if ($attribute_in_relation) {
171+
$column['attribute'] = $column['attribute'] ?? end($parts);
172+
}
173+
153174
return $column;
154175
}
155176
}

src/resources/views/crud/columns/select_multiple.blade.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
$column['limit'] = $column['limit'] ?? 40;
77
$column['attribute'] = $column['attribute'] ?? (new $column['model'])->identifiableAttribute();
88
9+
910
$results = data_get($entry, $column['name']);
1011
$results_array = [];
1112
12-
if(!$results->isEmpty()) {
13+
if($results !== null && !$results->isEmpty()) {
1314
$related_key = $results->first()->getKeyName();
1415
$results_array = $results->pluck($column['attribute'], $related_key)->toArray();
1516
}
@@ -43,4 +44,4 @@
4344
@else
4445
-
4546
@endif
46-
</span>
47+
</span>

tests/Unit/CrudPanel/CrudPanelColumnsTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ class CrudPanelColumnsTest extends BaseDBCrudPanelTest
135135
'type' => 'relationship',
136136
'key' => 'accountDetails__nickname',
137137
'priority' => 1,
138+
'attribute' => 'nickname',
138139
'tableColumn' => false,
139140
'orderable' => false,
140141
'searchLogic' => false,

0 commit comments

Comments
 (0)