Skip to content

Commit aa73ed4

Browse files
authored
Merge pull request #4512 from Laravel-Backpack/fix-column-attribute-for-filters
[Bug] Fix column attribute for filters
2 parents 68eeb08 + 7e1ffde commit aa73ed4

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,12 @@ public function makeSureColumnHasNeededAttributes($column)
372372
$column['relation_type'] = $this->inferRelationTypeFromRelationship($column);
373373
}
374374

375+
// if there's a model defined, but no attribute
376+
// guess an attribute using the identifiableAttribute functionality in CrudTrait
377+
if (isset($column['model']) && ! isset($column['attribute']) && method_exists($column['model'], 'identifiableAttribute')) {
378+
$column['attribute'] = (new $column['model'])->identifiableAttribute();
379+
}
380+
375381
// check if the column exists in the database (as a db column)
376382
$column_exists_in_db = $this->hasDatabaseColumn($this->model->getTable(), $column['name']);
377383

tests/Unit/CrudPanel/CrudPanelColumnsTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ class CrudPanelColumnsTest extends BaseDBCrudPanelTest
128128
'entity' => 'accountDetails',
129129
'model' => 'Backpack\CRUD\Tests\Unit\Models\AccountDetails',
130130
'relation_type' => 'HasOne',
131+
'attribute' => 'nickname',
131132
],
132133
'accountDetails__nickname' => [
133134
'name' => 'accountDetails.nickname',
@@ -155,6 +156,7 @@ class CrudPanelColumnsTest extends BaseDBCrudPanelTest
155156
'relation_type' => 'BelongsTo',
156157
'entity' => 'accountDetails.user',
157158
'model' => 'Backpack\CRUD\Tests\Unit\Models\User',
159+
'attribute' => 'name',
158160
],
159161
];
160162

@@ -278,6 +280,7 @@ class CrudPanelColumnsTest extends BaseDBCrudPanelTest
278280
'searchLogic' => false,
279281
'priority' => 0,
280282
'relation_type' => 'BelongsTo',
283+
'attribute' => 'content',
281284
],
282285
'ac_article_content' => [
283286
'name' => 'accountDetails.article',

0 commit comments

Comments
 (0)