Для демонстрации рюкзака я хотел бы показать идентификатор и название вместе. Для результата поиска нет проблем, выбранный в select2 также не проблема, проблема в том, что после сохранения он не отображается, он становится пустым.
Как я могу установить это?
Как ниже
рюкзак-демонстрация/приложение/Http/контроллеры/админ/MonsterCrudController.php
[ // select2_from_ajax: 1-n relationship
'label' => 'Select2_from_ajax', // Table column heading
'type' => 'select2_from_ajax',
'name' => 'select2_from_ajax', // the column that contains the ID of that connected entity;
'entity' => 'article', // the method that defines the relationship in your Model
'attribute' => 'id_title', // foreign key attribute that is shown to user
'model' => "Backpack\NewsCRUD\app\Models\Article", // foreign key model
'data_source' => url('api/article'), // url to controller search function (with /{id} should return model)
'placeholder' => 'Select an article', // placeholder for the select
'minimum_input_length' => 2, // minimum characters to type before querying results
'tab' => 'Relationships',
'wrapperAttributes' => ['class' => 'form-group col-md-12'],
],
рюкзак-демонстрация/приложение/Http/контроллеры/Api/ArticleController.php
if ($search_term) {
return Article::
selectRaw("CONCAT('Article ID: ', id,' | Title: ', Title) AS id_title, articles.*")
->where('title', 'LIKE', '%'.$search_term.'%')->paginate(10);
} else {
return Article::selectRaw("CONCAT('Article ID: ', id,' | Title: ', Title) AS id_title, articles.*")
->paginate(10);
}
Решение находится в Article Model
создать еще одну accessor
функцию
public function getIdTitleAttribute()
{
return 'Article ID: ' . $this->id . ' | Title: ' . $this->title;
}