laravel-admin select、multipleSelect从api中获取选项列表
友儿
发布
于
2022-09-11 12:47:00
发布
于
2022-09-11 12:47:00
select、multipleSelect从api中获取选项列表
$form->select($column[, $label])->options([1 => 'foo', 2 => 'bar', 'val' => 'Option name']);
或者从api中获取选项列表:
$form->select($column[, $label])->options('/api/users');
其中api接口的格式必须为下面格式:
"id": 9,
"text": "xxx"
"id": 21,
"text": "xxx"
如果选项过多,可通过ajax方式动态分页载入选项:
$form->select('user_id')->options(function ($id) {
$user = User::find($id);
if ($user) {
return [$user->id => $user->name];
})->ajax('/admin/api/users');
API /admin/api/users接口的代码:
public function users(Request $request)
$q = $request->get('q');
return User::where('name', 'like', "%$q%")->paginate(null, ['id', 'name as text']);
接口返回的数据结构为
"total": 4,
"per_page": 15,
"current_page": 1,
"last_page": 1,
"next_page_url": null,
"prev_page_url": null,
"from": 1,
"to": 3,
"data": [
"id": 9,
"text": "xxx"
"id": 21,
"text": "xxx"
"id": 42,
"text": "xxx"