laravel-admin.org 管理面板如何通过下拉菜单获取用户并查找用户

laravel-admin.org admin panel how to fetch users via dropdown menu and lookup users

我安装了 laravel-admin.org 然后在管理面板中我的默认表单是这样的:

这是我的部门管理员:在 /admin/ 文件夹中:

我有方法

一个用于网格视图:

/**
 * Make a grid builder.
 *
 * @return Grid
 */
protected function grid()
{
    $grid = new Grid(new Department());

    $grid->column('id', __('Id'));
    $grid->column('name', __('Name'));
    // $grid->column('owner_id', __('Owner id'));
    
    $grid->owner_id(__('models.name_of_user'))->display(function ($owner_id) {
        return ($owner_id ? User::find($owner_id)->name : null);
    });

    $grid->column('group_id', __('Group id'));
    $grid->column('limit', __('Limit'));
    $grid->column('created_at', __('Created at'));
    $grid->column('updated_at', __('Updated at'));

    return $grid;
}

一个用于详细查看:

/**
 * Make a show builder.
 *
 * @param mixed $id
 * @return Show
 */
protected function detail($id)
{
    $show = new Show(Department::findOrFail($id));

    $show->field('id', __('Id'));
    $show->field('name', __('Name'));
    $show->field('owner_id', __('Owner id'));
    $show->field('group_id', __('Group id'));
    $show->field('limit', __('Limit'));
    $show->field('created_at', __('Created at'));
    $show->field('updated_at', __('Updated at'));

    return $show;
}

还有一个用于编辑的表单视图:

/**
 * Make a form builder.
 *
 * @return Form
 */
protected function form()
{
    $form = new Form(new Department());
    $form->text('name', __('Name'));
    $form->number('owner_id', __('Owner id'));
    $form->number('group_id', __('Group id'));
    $form->number('limit', __('Limit'));

    return $form;
}

如何替换 下拉菜单 菜单以从用户模型中寻找用户?

如何在该下拉菜单中进行搜索,以便通过电子邮件查找和过滤我的用户?

在您的表单中:

    $form->select('owner_id', __('models.name_of_user'))->options(User::all()->pluck('name', 'id'));

您的详细信息:

    $users = User::all()->toArray();
    $usersArray = [];
    foreach ($users as $item) {
        $usersArray[$item['id']] = $item['name'];
    }
    $show->owner_id(__('models.name_of_user'))->using($usersArray);

在您的网格中:

    $grid->owner_id(__('models.name_of_user'))->display(function ($owner_id) {
        return ($owner_id ? User::find($owner_id)->name : null);
    });