PHP code example of keops007 / belongs-to-many-field

1. Go to this page and download the library: Download keops007/belongs-to-many-field library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

keops007 / belongs-to-many-field example snippets


use Keops007\BelongsToManyField\BelongsToManyField;

public function fields(Request $request){
    return [
        ..., //If you are using with BelongsToMany native Field, put this field after

        BelongsToManyField::make('Role Label', 'roles', 'App\Nova\Role'),
    ];
}

use Keops007\BelongsToManyField\BelongsToManyField;

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

public function handle(ActionFields $fields, Collection $models)
{
    //note that roles is the many to many relationship function name
    $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
     ]);

     BelongsTo::make('Association', 'association', 'App\Nova\Association'),

     BelongsToManyField::make('Participants', 'participant', 'App\Nova\Participant')
     ->dependsOn('association', 'association_id'),

     BelongsToManyField::make('Participants', 'participant', 'App\Nova\Participant')
     ->canSelectAll('Seleccionar Todo'),

use Keops007\BelongsToManyField\BelongsToManyField;

public function fields(Request $request){
    return [
        ...,
        BelongsToManyField::make('Role Label', 'roles', 'App\Nova\Role')->relationModel(\App\User::class)->rules('