Solve the problem that select in laravel-admin cannot automatically select the current value when editing the form

不言
Release: 2023-04-02 18:28:01
Original
4979 people have browsed it

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.

For example, create a method to generate options for the model User.php

    /**
     * 获取用户列表-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;
    }
Copy after login

Use it in the corresponding controller, such as the controller of Article

    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());
            (略)
Copy after login

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'));
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template