Laravel Nova BelongsToMany在资源索引中显示

时间:2019-11-01 18:00:38

标签: laravel laravel-nova

我有以下模特和新星资源。在我的记录nova资源上,我想显示记录中附带的每个运动员的名字和姓氏。我可以在详细页面上看到运动员,但我希望他们在资源索引页面上。这可能吗?

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'),

        ];
    }

2 个答案:

答案 0 :(得分:1)

使用我的nova软件包:lathanhvien/custom-belongs-to-many-field,从Benjacho / belongs-to-manyfield-nova软件包扩展而来。它将在Record Nova资源上显示(共3页:索引,详细信息,表格)运动员的名字和姓氏(或任何其他列)。

遵循此设置:

// 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'])

        ];
    }

答案 1 :(得分:0)

您还可以使用计算字段,如下所示:

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