z-song / laravel-admin

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

RowAction 传递其他参数 #5720

Open cjango opened 1 year ago

cjango commented 1 year ago

Description:

使用 RowAction 的时候,只能获取到当前行的数据,怎样能附加一些其他数据过去呢? 很多时候,操作的数据不仅仅是当前行

// 考虑过使用构造函数传参,但是并不能执行
$actions->add(new GroupMuteUser($group));

use Encore\Admin\Actions\RowAction;

class GroupMuteUser extends RowAction
{
    public $name = '禁言成员';

    protected $group;

    public function __construct($group)
    {
        $this->group = $group;
    }

    public function handle()
    {
        dump($this->group);
        // 此时group提示未初始化
    }
}
alexoleynik0 commented 1 year ago

Can't test your example with constructor right now, but speaking of "more data" in the RowAction -- you can make any data fetching inside handle method directly.

Try make it static property if you want to only load a big data chunk for many rows handling.