dcatadmin中表单提交的时候可以新增一个字段保存吗

问题描述

使用的dcatadmin2.x版本
在表单提交的时候,我想设置一个user_id的字段,一起保存到数据库。但是试了各种方法都没用

方法1:

在保存前回调的时候设置user_id

$form->saving(function (Form $form) {
    // 1
    $form->model()->user_id = auth('admin')->id();
    // 2
    $form->input('user_id', auth('admin')->id());
});

方法2

在model文件中设置user_id

protected static function booted()
{
    static::creating(function ($model) {
        if (auth()->check()) {
            $model->user_id = auth('admin')->id(); // 自动注入当前用户ID
        }
    });
}

方法3

按照官方文档的写法,在保存后回调

$form->saved(function (Form $form, $result) {
    // 在表單保存後獲取eloquent
    $form->model()->update(['data' => 'new']);
});

以上3种写法都不行,最后只能是

$form->saved(function (Form $form, $result) {
     \App\Models\YkAccount::where(['id' => $form->getKey()])->update(['user_id' => auth('admin')->id()]);
});

这样才可以。

但是在保存后回调里面再更新user_id,这是两步,有可能保存成功了,更新user_id失败,这就会导致user_id没有值。

而且我看了文档submitted、saving都只是支持修改、删除用户提交的数据或者中断提交操作,并不支持新增。

想请问下大家有新增的方法吗?或者有遇到过类似问题的同学,麻烦提供下解决思路和建议,谢谢了

环境

dcatadmin2.x
laravel9
php8

命中水
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 7
Dcatplus-杨光

使用表单隐藏域不行吗?

file

2周前 评论
命中水 (楼主) 2周前

可以重写数据仓库里面的 增删改查,

/**
     * 查找数据
     * @param Model $model
     * @return array|\Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection
     */
    public function get(Model $model)
    {
        $model->whereHas('userInfoTenant', function ($query) {
            $query->where('tenant_id', Tenancy::adminTenantId());
        });
        return parent::get($model);
    }


    /**
     * 查询编辑页面数据.
     *
     * @param Form $form
     * @return array|\Illuminate\Contracts\Support\Arrayable
     */
    public function edit(Form $form)
    {
        $query = $this->newQuery();

        if ($this->isSoftDeletes) {
            $query->withTrashed();
        }

        $this->model = $query
            ->whereHas('userInfoTenant', function ($query) {
                $query->where('tenant_id', Tenancy::adminTenantId());
            })
            ->with($this->getRelations())
            ->findOrFail($form->getKey(), $this->getFormColumns());

        return $this->model;
    }

    /**
     * 更新数据
     * @param Form $form
     * @return bool|null
     */
    public function update(Form $form)
    {
        if ($this->edit($form)->userInfoTenant->tenant_id != Tenancy::adminTenantId()) {
            return false;
        }
        return parent::update($form);
    }

    /**
     * 删除数据
     * @param Form $form
     * @param array $originalData
     * @return bool
     */
    public function delete(Form $form, array $originalData)
    {
        $models = $this->collection->keyBy($this->getKeyName());
        collect(explode(',', $form->getKey()))->filter()->each(function ($id) use ($form, $models) {
            $model = $models
                ->whereHas('userInfoTenant', function ($query) {
                    $query->where('tenant_id', Tenancy::adminTenantId());
                })
                ->get($id);
            if (!$model) {
                return;
            }
            $data = $model->toArray();
            if ($this->isSoftDeletes && $model->trashed()) {
                $form->deleteFiles($data, true);
                $model->forceDelete();
                return;
            } elseif (!$this->isSoftDeletes) {
                $form->deleteFiles($data);
            }
            $model->delete();
        });
        return true;
    }

    /**
     * 显示数据
     * @param Show $show
     * @return array
     */
    public function detail(Show $show)
    {
        $query = $this->newQuery();
        if ($this->isSoftDeletes) {
            $query->withTrashed();
        }
        $this->model = $query
            ->whereHas('userInfoTenant', function ($query) {
                $query->where('tenant_id', Tenancy::adminTenantId());
            })
            ->with($this->getRelations())
            ->findOrFail($show->getKey(), $this->getDetailColumns());

        return $this->model;
    }
2周前 评论
Mutoulee

首先,你需要表单埋一个隐藏域,并且不要设置value:

$form->hidden('user_id');

然后在表单动作事件中:$form->input('user_id', auth('admin')->id());

2周前 评论

我第一个想到的是 hidden

2周前 评论
DogLoML

dcat控制器的store方法在保存前会过滤掉form中没有的字段,你可以加一个空值的隐藏域,然后在saving中修改应该就可以了。

2周前 评论
DogLoML (作者) 2周前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!