Chris Muench
Chris Muench

Reputation: 18318

Laravel Nova BelongsToMany display in resource index

I have the following models & nova resources. On my records nova resource I would like to display the first and last name of each athlete attached to the record. I can see the athletes on the detailed page, but I would like them on the resource index page. Is this possible?

namespace App;

class Record extends Model
{
    use SoftDeletes;
    protected $guarded = ['id'];

    public function athletes()
    {
        return $this->BelongsToMany('App\Athlete');
    }
}


class Athlete extends Model
{
    use SoftDeletes;
    protected $guarded = ['id'];

    public function records()
    {
       return $this->belongsToMany('App\Record');
    }
}

class Record extends Resource
{
    /**
     * The logical group associated with the resource.
     *
     * @var string
     */
        public static $group = 'Records';

    /**
     * The model the resource corresponds to.
     *
     * @var string
     */
    public static $model = 'App\Record';

    /**
     * The single value that should be used to represent the resource when being displayed.
     *
     * @var string
     */
    public static $title = 'id';

    /**
     * The columns that should be searched.
     *
     * @var array
     */
    public static $search = [
        'id','value','notes'
    ];

        /**
         * The relationship columns that should be searched.
         *
         * @var array
         */
        public static $searchRelations = [
        'event_school.school' => ['name'],
        'event_school.event' => ['name'],
        'athletes' => ['first_name','last_name'],
        ];


    /**
     * Get the fields displayed by the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),
                        BelongsToMany::make('Athletes')->sortable(),                        
                        BelongsTo::make('School Event','event_school','App\Nova\EventSchool')->sortable()->searchable(),
                        Boolean::make('Archived')->sortable(),
                        Text::make('Value')->sortable(),
                        Select::make('Value Type')->options([
                            'time' => 'Time',
                            'distance' => 'Distance',
                        ])->sortable(),
                        Select::make('Display Measurement')->options([
                            'metric' => 'Metric',
                            'imperial' => 'Imperial',
                        ])->sortable(),

                        Text::make('Year')->sortable(),
                        Text::make('Place')->sortable(),
                        Text::make('Notes')->sortable(),

                        BelongsToMany::make('Attributes')->fields(function() {
                    return [
                        Text::make('Value'),
                                Select::make('Value Type')->options([
                                    'time' => 'Time',
                                    'distance' => 'Distance',
                                ])->sortable(),
                                Select::make('Display Measurement')->options([
                                    'metric' => 'Metric',
                                    'imperial' => 'Imperial',
                                ])->sortable()
                    ];
                    })                      
        ];
    }
}


class Athlete extends Resource
{
    /**
     * The logical group associated with the resource.
     *
     * @var string
     */
        public static $group = 'Records';

    /**
     * The model the resource corresponds to.
     *
     * @var string
     */
    public static $model = 'App\Athlete';

    /**
     * The single value that should be used to represent the resource when being displayed.
     *
     * @var string
     */
    public static $title = 'first_name';

    /**
     * The columns that should be searched.
     *
     * @var array
     */
    public static $search = [
        'id','first_name','last_name'
    ];

        /**
         * The relationship columns that should be searched.
         *
         * @var array
         */
        public static $searchRelations = [
            'school' => ['name'],
        ];

    /**
     * Get the fields displayed by the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),
                        BelongsTo::make('School'),
                        BelongsToMany::make('Records'),
              Text::make('First Name')
                  ->sortable()
                  ->rules('required', 'max:255'),
              Text::make('Last Name')
                  ->sortable()
                  ->rules('required', 'max:255'),

        ];
    }

Upvotes: 2

Views: 4878

Answers (2)

pzmarzly
pzmarzly

Reputation: 827

You could have also used a computed field, like so:

Text::make('Athletes', function () {
    return $this->athlete->map(function ($athlete) {
        return $athlete->first_name . ' ' . $athlete->last_name);
    })->join(', ');
})->onlyOnIndex(),

Upvotes: 8

protoproto
protoproto

Reputation: 2099

Use my nova packages: lathanhvien/custom-belongs-to-many-field, extend from Benjacho/belongs-to-many-field-nova package. It will show (at full 3 pages: index, detail, form) Athlete first and last name (or any other columns) on Record Nova resource.

Follow this setup:

// app\Nova\Record.php
use Pifpif\CustomBelongsToManyField\CustomBelongsToManyField;
...
    public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),
            Text::make('Value'),
            Text::make('Year'),
            Text::make('Place'),
            CustomBelongsToManyField::make('Athletes', 'athletes', 'App\Nova\Athlete')
                ->optionsLabel('first_name')
                ->optionsShow(['first_name','last_name'])

        ];
    }

Upvotes: 2

Related Questions