# belongsToMany

![](https://2944208205-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-McfaOoX81ScSMpXsVTN%2F-MdlF3cJGdNAHti05Ljt%2F-MdlIxzLzagzmUrPZQmw%2FbelongsToMany-field.png?alt=media\&token=3a259dab-5362-4ef7-b99e-2d742a6fe550)

```php
$this->addFormItem([
    'id' => 'categories',
    'parent_id' => 'right-col',
    'type' => 'belongsToMany',
    'relation_name' => 'categories',
    'model' => '\App\Models\Category',
    'where' => function($query){
        return $query->where('is_active', 1);
    },
    'show_select_from_table_button' => true,
    'show_add_new_button' => true,
    'validation_rules' => ['categories' => 'required|array|min:1'],
    'label' => __('Categories'),
    'name' => 'categories'
]);
```
