【问题标题】:How to define seeder that creates m2m relationship on Voyager Laravel project?如何定义在 Voyager Laravel 项目上创建 m2m 关系的播种机?
【发布时间】:2020-09-22 18:18:48
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: laravel seeding eloquent-relationship voyager


    【解决方案1】:
    $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',
    ];
    

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

    干杯

    【讨论】:

      猜你喜欢
      • 2018-07-22
      • 2018-06-22
      • 2020-04-06
      • 2018-05-26
      • 2016-05-28
      • 1970-01-01
      • 1970-01-01
      • 2020-11-14
      • 2017-12-08
      相关资源
      最近更新 更多