This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

Mathilde Escher Stiftung
Mathilde Escher Stiftung

Hallo Is it possible to have a dropdown instead select boxes for displaying my pivot data.

This is what I have so far:

Many to many relationship for my milestones categories

public $belongsToMany = [
        'category' => [
            'CreationHandicap\Milestones\Models\Category',
            'table'         => 'creationhandicap_milestones_milestone_category',
            'order'         => 'title',
        ]
];

My fields.yaml with category relation

fields:
    category:
        label: Kategorie
        span: full
        nameFrom: title
        descriptionFrom: description
        type: relation

I could change the field to a type dropdown and have my category options in the model like that

public function getCategoryOptions($value, $formData)
    {
        return CategoryModel::all()->lists('title', 'id');
    }

But the I get a array to string conversion error for the selected value. How can I avoid this?

Last updated

daftspunky
daftspunky

This relation type belongsToMany allows multiple selections, this is why checkboxes are given. If you change the relationship to belongsTo then a dropdown will be displayed

The reason for the error is because the relationship also expects an array of items, not a scalar

If you really want to overcome this, you could use an event to cast to an array in your model

function beforeSave() 
{
    // Cast to an array to fix array to string conversion error
    $this->category = (array) $this->category;
}

Hope this helps

Mathilde Escher Stiftung
Mathilde Escher Stiftung

Yes, of course! belongsToMany makes completely no sense, when wanting to have a dropdown. I changed it to belongsTo, added a category_id field in my table and it worked like a charm.

Thanks!

1-3 of 3

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.