Linked Questions

Popular Questions

backpack-for-laravel n-n relationship and select-multiple fields

Asked by At

I have three tables, accomodations, rooms and accommodation_rooms.

In rooms I have id and room_name fields, together with timestamp create/modified fields and other setting fields.

The third table is the xref table, with the two accomodation_id and room_id fields.

As explained here: https://laravel.com/docs/5.7/eloquent-relationships#many-to-many I setup my rooms method in my accomodation model explicitly:

    public function rooms()
    {
        return $this->belongsToMany('App\Models\Room', 'accomodation_rooms', 'accomodation_id', 'room_id');
    }

Then I try to setup a field like this:

    $room = [
        [       // Select2Multiple = n-n relationship (with pivot table)
        'label' => "Rooms",
        'type' => 'select2_multiple',
        'name' => 'rooms', // the method that defines the relationship in your Model
        'entity' => 'rooms', // the method that defines the relationship in your Model
        'attribute' => 'room_name', // foreign key attribute that is shown to user
        'model' => "App\Models\Rooms", // foreign key model
        'pivot' => true, // on create&update, do you need to add/delete pivot table entries?
        ]
    ];

    $this->crud->addField($room);

But I get the following error on vendor/backpack/crud/src/PanelTraits/Fields.php, line 37:

Undefined index: name

where I have this code:

    // if the label is missing, we should set it
    if (! isset($completeFieldsArray['label'])) {
        $completeFieldsArray['label'] = mb_ucfirst(str_replace('_', ' ', $completeFieldsArray['name']));
    }

Please help.

Laravel 5,7, backpackforlaravel 3.5.

Related Questions