z-song / laravel-admin

Build a full-featured administrative interface in ten minutes
https://laravel-admin.org
MIT License
11.12k stars 2.81k forks source link

How to use relationships in RowAction #5763

Open phuchung2399 opened 1 year ago

phuchung2399 commented 1 year ago

Description:

Currently, I want to use relationships in RowAction like Form() in Laravel-admin. But now I don't know how.

ex:

image

Looking forward to everyone's support, thanks.

technilogics commented 1 year ago

As per document a very limited support is available for form in row action, even linking is not available.

Yanghsuanming commented 5 months ago
<?php

namespace App\Admin\Extensions;

use Illuminate\Database\Eloquent\Model;
use Encore\Admin\Actions\Interactor\Dialog;
use Encore\Admin\Actions\Interactor\Form;
use Encore\Admin\Actions\RowAction;

class ReplyButton extends RowAction
{
    public $name = 'RowEditor';

    protected $selector = '.import-post';

    /**
     * @var Interactor\Interactor
     */
    protected $interactor;

    /**
     * Action constructor.
     */
    public function __construct()
    {
        $this->initInteractor();
    }

    /**
     * @throws \Exception
     */
    protected function initInteractor()
    {
        if ($hasForm = method_exists($this, 'form')) {
            $this->interactor = new Form($this);
        }

        if ($hasDialog = method_exists($this, 'dialog')) {
            $this->interactor = new Dialog($this);
        }

        if ($hasForm && $hasDialog) {
            throw new \Exception('Can only define one of the methods in `form` and `dialog`');
        }
    }

    public function handle(Model $model)
    {
        // \Log::info($this->getKey());
        // // 下面的代码获取到上传的文件,然后使用`maatwebsite/excel`等包来处理上传你的文件,保存到数据库
        // // $request->file('file');
        // $id = $this->getKey();
        $model->title = request()->input('title');
        $model->save();
        return $this->response()->success("导入完成!")->refresh();
    }

    public function form()
    {
        // $this->hidden('id')->value($this->getKey());
        $this->text('title', __('標題'))->rules('required')->value($this->getRow()->title);
        $this->textarea('message', __('內文'))->rules('required')->value($this->getRow()->message);
    }

    /**
     * @return string
     */
    public function render()
    {
        $this->addScript();

        $modalId = '';

        if ($this->interactor instanceof Form) {
            $modalId = $this->interactor->getModalId();

            // if ($content = $this->form()) {
            //     return $this->interactor->addElementAttr($content, $this->selector);
            // }
        }

        return sprintf(
            "<a data-_key='%s' href='javascript:void(0);' class='%s' %s>%s</a>",
            $this->getKey(),
            $this->getElementClass(),
            $modalId ? "modal='{$modalId}'" : '',
            $this->name()
        );
    }
}