Home > Article > Backend Development > Solve the problem that select in laravel-admin cannot automatically select the current value when editing the form
This article mainly introduces how to solve the problem that select in laravel-admin cannot automatically select the current value when editing the form. It has a certain reference value. Now I share it with you. Friends in need can refer to it
Just create a method for each Model that can automatically generate a select option.
/** * 获取用户列表-select-option * @return User[]|\Illuminate\Database\Eloquent\Collection */ public static function getSelectOptions() { $options = DB::table('users')->select('id','name as text')->get(); $selectOption = []; foreach ($options as $option){ $selectOption[$option->id] = $option->text; } return $selectOption; }
protected function form() { return Admin::form(Article::class, function (Form $form) { $form->display('id', 'ID'); $form->text('title','标题')->rules('required|min:10'); $form->textarea('description','摘要简介'); $form->ueditor('body','正文')->rules('min:10'); $form->select('user_id','作者')->options(User::getSelectOptions()); (略)
Use as mentioned in the document The interface method, and the format in the returned document cannot automatically select the original value, it can only be blank.
$form->select('user_id','作者')->options(admin_base_path('/api/users'));
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
Methods to update openssl, cur and php in Centos
Performance of Laravel-permission project Optimization
Using Swoole’s coroutine database query in Laravel 5.6
The above is the detailed content of Solve the problem that select in laravel-admin cannot automatically select the current value when editing the form. For more information, please follow other related articles on the PHP Chinese website!