如何定义在 Voyager Laravel 项目上创建 m2m 关系的播种机?

How to define seeder that creates m2m relationship on Voyager Laravel project?

资源有名称 以及 m2m 与术语的关系 例如,资源可能是一本书,术语可能是作者。 这是我创建的播种机的一部分,用于在我的项目中自动创建面包。

表格是资源、术语和 resource_term resource_term 有 id,resource_id,term_id

$resDataType = DataType::where('slug', 'resources')->firstOrFail();
        $dataRow = $this->dataRow($resDataType, 'term_belongstomany_resource_relationship');
        if (!$dataRow->exists) {
            $dataRow->fill([
                'type'         => 'relationship',
                'display_name' => 'Término',
                'required'     => 1,
                'browse'       => 1,
                'read'         => 1,
                'edit'         => 1,
                'add'          => 1,
                'delete'       => 0,
                'details'      => '{"model":App\BizneUp\Domain\Model\Term,"table":"terms","type":"belongsToMany","column":"term_id","key":"id","label":"name","pivot_table":"resource_term","pivot":"0"}',
                'order'        => 12,
            ])->save();
        }

当我打开面包时它抛给我的错误是这样的:

array_diff_key():参数 #1 不是数组(视图:/home/rush4u/projects/bizneup/bizneup_backend/vendor/tcg/voyager/resources/views/tools/bread/relationship-partial.blade.php)(视图:/home/rush4u/projects/bizneup/bizneup_backend/vendor/tcg/voyager/resources/views/tools/bread/relationship-partial.blade.php)

航海者模板的这一行: $adv_details = array_diff_key(json_decode(json_encode($relationshipDetails), true), $relationshipKeyArray);

如果有人做过这些类型的事情,请帮帮我 :)

$dataRow = $this->dataRow($userDataType, 'resource_belongstomany_term_relationship');
        if (!$dataRow->exists) {
            $dataRow->fill([
                'type'         => 'relationship',
                'display_name' => 'Términos',
                'required'     => 0,
                'browse'       => 1,
                'read'         => 1,
                'edit'         => 1,
                'add'          => 1,
                'delete'       => 0,
                'details'      => [
                    'model'       => Term::class,
                    'table'       => 'terms',
                    'type'        => 'belongsToMany',
                    'column'      => 'id',
                    'key'         => 'id',
                    'label'       => 'name',
                    'pivot_table' => 'resource_term',
                    'pivot'       => '1',
                    'taggable'    => '0',
                ],
                'order'        => 12,
            ])->save();
        }

M2M 关系的详细说明

'details' => [
    'model'       => Term::class, //model class
    'table'       => 'terms', // table you are related to
    'type'        => 'belongsToMany', // rel name
    'column'      => 'id', // id of main side entity of rel
    'key'         => 'id', // id of related side entity
    'label'       => 'name', // field to act as toString when displaying the relation on read, browse, etc.
    'pivot_table' => 'resource_term', // m2m table name
    'pivot'       => '1',
    'taggable'    => '0',
];

希望这可以帮助面临同样问题的人(手动设置 voyager 关系)

干杯