ST80
ST80

Reputation: 3903

Laravel Nova How to create a select with options based on other Nova Resouce

Is it possible to create Select options from an other Nova resource?

I tried to do this:

Select::make('Contactperson')
     ->rules('required')
     ->options(// Here I want the values from the "Employees"-resource )
     ->displayUsingLabels()
     ->sortable()

I looked through the documentation and did not find anything about this, maybe there is some way around?

Upvotes: 3

Views: 3879

Answers (2)

kdrmlhcn
kdrmlhcn

Reputation: 96

try like this

->options(Employees::all()->pluck('name', 'id'))

Upvotes: 3

Oleksandr Roskovynskyi
Oleksandr Roskovynskyi

Reputation: 429

It is better to approach the BelongsTo field for this:

https://nova.laravel.com/docs/2.0/resources/relationships.html#belongsto

use Laravel\Nova\Fields\BelongsTo;

BelongsTo::make('Contactperson', 'your_relation_method', 'App\Models\Employees')
    ->rules('required')
->sortable();

Upvotes: 1

Related Questions