bab55z / belongs-to-many-field-nova

Belongs To Many field Laravel nova to represent many to many relationship in field.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Belongs To Many Field Nova

Belongs To Many field to represent many to many relationship in field. This Field allow attaching relationships easily, you can pass query to the Multiple Select.

image

Installation

composer require benjacho/belongs-to-many-field

Deprecation

Method relationModel() no more needed, to prevent conflicts it will be there. And trait HasBelongsToMany no more neede too, both will be in repo, but doesn't work.

Method options is not needed anymore.

Usage

To use in nova 1.0 use 0.3 in nova 2.0 use 0.4 and above.

In the resource you need to pass:

  • Method make (label, many to many relationship, Nova Resource Relationship)
  • It is available in index, detail and forms!
use Benjacho\BelongsToManyField\BelongsToManyField;

public function fields(Request $request){
    BelongsToManyField::make('Role Label', 'roles', 'App\Nova\Role'),
}

Optional

  • Method optionsLabel('columnName'), this method is when you don't have column 'name' in your table and you want to label by another column name. By default it tracks by label 'name'.

IMPORTANT

  • If you want to label by another column name, you need to set the title() method on your resource, this method returns an string that is used to label it, also don't forget to add optionsLabel() method.
use Benjacho\BelongsToManyField\BelongsToManyField;

public function fields(Request $request){
    BelongsToManyField::make('Role Label', 'roles', 'App\Nova\Role')->optionsLabel('title'),
}
  • Method isAction(), this method is when you need this field in actions, this puts height of field in 350px, and converts in action.
use Benjacho\BelongsToManyField\BelongsToManyField;

public function fields(Request $request){
    BelongsToManyField::make('Role Label', 'roles', 'App\Nova\Role')->isAction(),
}

To obtain the data that is send in action do it:

public function handle(ActionFields $fields, Collection $models)
{
    // Get the expenseTypes from the request because the Field BelongsToManyField does not send it
    
    $values = array_column(json_decode(request()->roles, true),'id');
    
    foreach ($models as $model) {
        $model->roles()->sync($values);
    }
}
     BelongsToManyField::make('Role Label', 'roles', 'App\Nova\Role')
     ->options(\App\Role::all()
     ->setMultiselectProps([
        'selectLabel' => 'click for select',
        // and others from docs
     ]);

Validations

This package implement all Laravel Validations, you need to pass the rules in rules method, rules are listed on laravel validations rules for arrays*.

use Benjacho\BelongsToManyField\BelongsToManyField;

public function fields(Request $request){
    BelongsToManyField::make('Role Label', 'roles', 'App\Nova\Role')->relationModel(\App\User::class)->rules('required', 'min:1', 'max:5', 'size:3' new CustomRule),
}

image

For translations of this validations, use normal laravel validations translations.

Todo

Implement validations, implement custom rules

Contributing

-Pull Requests -Issues -Or Contact me: christianbfc97@gmail.com

About

Belongs To Many field Laravel nova to represent many to many relationship in field.

License:MIT License


Languages

Language:PHP 61.4%Language:Vue 35.1%Language:JavaScript 3.3%Language:CSS 0.1%