laravel-admin icon indicating copy to clipboard operation
laravel-admin copied to clipboard

Selectable: how to pass variable/parameter filter to make/selectable/modal? [question]

Open APMU opened this issue 3 years ago • 2 comments

Hi,

Having, for sample, this:

At create a new ARTICLE have the field AUTHOR (many2many relation). At select others ARTICLES from same AUTHOR (manually, specific), by make/selectable/modal, how to pass parameter/filter to list only the ARTICLES from same AUTHOR, previously selected at the ARTICLE form?

APMU avatar Dec 24 '21 12:12 APMU

这里有一个belongsTo的解决方案,供参考:在app\Admin\Controllers\YourModelController里重写Encore\Admin\Controllers\AdminControlleredit($id)create()方法: 原始的:

    /**
     * Edit interface.
     *
     * @param mixed $id
     *
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->title($this->title())
            ->description($this->description['edit'] ?? trans('admin.edit'))
            ->body($this->form()->edit($id))
        ;
    }
    /**
     * Create interface.
     *
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->title($this->title())
            ->description($this->description['create'] ?? trans('admin.create'))
            ->body($this->form())
        ;
    }

分别重写为

    /**
     * Edit interface.
     *
     * @param mixed $id
     *
     * @return Content
     */
    public function edit($id, Content $content)
    {
        // 解决关联关系 Redirect()->back()->withInput() 字段丢失的问题
        $form = $this->form()->edit($id);

        foreach ($form->fields() as $field) {
            if (/*$field instanceof BelongsTo && */session()->hasOldInput($field->column())) {
                $field->default(session()->getOldInput($field->column()));
                $field->value(session()->getOldInput($field->column()));
            }
        }

        return $content
            ->title($this->title())
            ->description($this->description['edit'] ?? trans('admin.edit'))
            ->body($form)
        ;
    }
/**
     * Create interface.
     *
     * @return Content
     */
    public function create(Content $content)
    {
        // 解决关联关系 Redirect()->back()->withInput() 字段丢失的问题
        $form = $this->form();

        foreach ($form->fields() as $field) {
            if (/*$field instanceof BelongsTo && */session()->hasOldInput($field->column())) {
                $field->default(session()->getOldInput($field->column()));
                $field->value(session()->getOldInput($field->column()));
            }
        }

        return $content
            ->title($this->title())
            ->description($this->description['create'] ?? trans('admin.create'))
            ->body($form)
        ;
    }

masterwto avatar Feb 27 '22 05:02 masterwto

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Apr 28 '22 09:04 stale[bot]