laravel - Laravel Backpack select2_from_ajax 选择更改结果和默认值
问题描述
对于背包演示,我想将 ID 和 Title 一起显示。搜索结果没有问题,在select2处选择也没有问题,问题是保存后不显示,变成空白。
我该如何设置?
背包演示/app/Http/Controllers/Admin/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'],
],
背包-demo/app/Http/Controllers/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;
}
推荐阅读
- python - 在 scipy 中从 solve_ivp 捕获 UserWarnings(求解 ODE)
- c# - 出现“无法隐式转换类型”错误但无法弄清楚原因
- jasper-reports - 带有其他子报表的子报表总是更改页面而不是使用剩余空间
- c# - Microsoft.EntityFrameworkCore.Diagnostics.RelationalStrings.get_InvalidMaxBatchSize()
- verilog - 例如 uut/A1/,正式端口 S 的宽度 1 不等于实际信号 in1 的宽度 4
- python - 为什么我的 python 代码出现值错误
- machine-learning - 如何在 CatBoostRegressor 中传递分类特征?
- java - 如何将一串http请求参数转换为列表
- python - 获取python中的json路径列表
- c++ - 冒泡排序只产生一个随机数而不是排序