Dact-admin 一对多关联新增报错 array_key_exists(): Argument #2 ($array) must be of type array, string give

1. 运行环境

nginx
dcat-admin 2.2

1). 当前使用的 Laravel 版本?

8.5

2). 当前使用的 php/php-fpm 版本?

PHP 版本:
php 8.0

3). 当前系统

macOS

4). 业务环境

开发环境

2. 问题描述?

Dact-admin 一对多关联新增报错 array_key_exists(): Argument #2 ($array) must be of type array, string give

Dact-admin 一对多关联新增报错 array_key_exists(): Argument #2 ($array) must be of type array, string give

form

![Dact-admin 一对多关联新增报错 array_key_exists(): Argument #2 ($array) must be of type array, string give]

关联关系

Dact-admin 一对多关联新增报错 array_key_exists(): Argument #2 ($array) must be of type array, string give

Dact-admin 一对多关联新增报错 array_key_exists(): Argument #2 ($array) must be of type array, string give

Dact-admin 一对多关联新增报错 array_key_exists(): Argument #2 ($array) must be of type array, string give

尝试在saving里修改类型也是无效

Dact-admin 一对多关联新增报错 array_key_exists(): Argument #2 ($array) must be of type array, string give

不使用关联关系提交

 $form->tree('fares', '不包邮区域')
                    ->nodes((new RegionService())->provinces())
                    ->setTitleColumn('title');
                $form->number('freight', '运费')->required();

![Dact-admin 一对多关联新增报错 array_key_exists(): Argument #2 ($array) must be of type array, string give]

protected function form(): Form
    {
        return Form::make(new FareTemplate(), function (Form $form) {
            $form->display('id');
            $form->text('name');
            $form->radio('type', '邮费类型')->options(FareTemplate::TYPE_MAP)->default(0)->when(1, function (Form $form) {
                $form->tree('conditions', '不包邮区域')
                    ->nodes((new RegionService())->provinces())
                    ->setTitleColumn('title');
                $form->number('freight', '运费')->required();
            })->when(0, function (Form $form) {
                $form->number('freight', '运费')->required();
            });
            $form->text('storehouse', '发货仓')->default('')->placeholder('杭州仓库');
            $form->text('delivery_time', '发货时间')->default('')->placeholder('24小时内发货');
        });
    }
// templateModel
public function conditions(): HasMany
{
        return $this->hasMany(FareTemplateCondition::class, 'template_id');
}

// templateConditionModel
public function template(): BelongsTo
{
        return $this->belongsTo(FareTemplate::class, 'template_id');
}
DaiChongWeb
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
最佳答案

可能是我太菜,历经昨天下午到现在,终于找到解决办法了!!!!! 首先在一对多的字段上增加saving,使用array_walk或者其他方法实现组装附表(多)的字段值

 $form->tree('conditions')->nodes((new RegionService())->provinces())
           ->setTitleColumn('title')->saving(function ($v) {
                array_walk($v, function (&$item) {
                     $item = ['province' => $item];
                });
                return $v;
           });

然后就是关联关系,只写一个外键

public function conditions(): HasMany
{
       return $this->hasMany(FareTemplateCondition::class, 'template_id');
}
public function template(): BelongsTo
{
      return $this->belongsTo(FareTemplate::class, 'template_id');
}
1年前 评论
讨论数量: 6
Mutoulee

文档中一对多表单用的是$form->hasMany 不过我自己laravel9+dcat admin 2.2 也是报错。

1年前 评论
daichongweb (楼主) 1年前

file 我一对多直接保存成json呢
而且你前面保存的是conditions不应该用explode这是切割成数组了 你直接implode或者转换成json试试

1年前 评论
daichongweb (楼主) 1年前
daichongweb (楼主) 1年前

可能是我太菜,历经昨天下午到现在,终于找到解决办法了!!!!! 首先在一对多的字段上增加saving,使用array_walk或者其他方法实现组装附表(多)的字段值

 $form->tree('conditions')->nodes((new RegionService())->provinces())
           ->setTitleColumn('title')->saving(function ($v) {
                array_walk($v, function (&$item) {
                     $item = ['province' => $item];
                });
                return $v;
           });

然后就是关联关系,只写一个外键

public function conditions(): HasMany
{
       return $this->hasMany(FareTemplateCondition::class, 'template_id');
}
public function template(): BelongsTo
{
      return $this->belongsTo(FareTemplate::class, 'template_id');
}
1年前 评论

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