Search code examples
laravelseedingeloquent-relationshipvoyager

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


A resource has name and a relationship of m2m with terms For instance a resource might be a book, and a term might be an author. This is part of a seeder that im creating to automate the bread creation on my project.

the tables are resource, terms, and resource_term resource_term has id, resource_id, and 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();
        }

The error it throws at me when i open the bread is this:

array_diff_key(): Argument #1 is not an array (View: /home/rush4u/projects/bizneup/bizneup_backend/vendor/tcg/voyager/resources/views/tools/bread/relationship-partial.blade.php) (View: /home/rush4u/projects/bizneup/bizneup_backend/vendor/tcg/voyager/resources/views/tools/bread/relationship-partial.blade.php)

On this line of the voyager template: $adv_details = array_diff_key(json_decode(json_encode($relationshipDetails), true), $relationshipKeyArray);

If anyone has done these types of things give me a hand please :)


Solution

  • $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();
            }
    

    Details explained for M2m relationships

    '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',
    ];
    

    Hope this helps people facing the same problem (setting up voyager relationships manually)

    cheers