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
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
最佳答案

可能是我太菜,历经昨天下午到现在,终于找到解决办法了!!!!! 首先在一对多的字段上增加 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');
}
2年前 评论
讨论数量: 6
Mutoulee

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

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

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

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

可能是我太菜,历经昨天下午到现在,终于找到解决办法了!!!!! 首先在一对多的字段上增加 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');
}
2年前 评论