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

问题描述 投票:0回答:1

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

这些表是资源,术语和resource_termresource_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);

[如果有人做过这类事情,请帮我忙:)

laravel seeding eloquent--relationship voyager
1个回答
0
投票
$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',
];

希望这可以帮助面临相同问题的人(手动设置航海者关系)

欢呼声

© www.soinside.com 2019 - 2024. All rights reserved.