dillingham / nova-ajax-select

Ajax select / child select package for Laravel Nova
MIT License
87 stars 22 forks source link

Save resource > wrong attribute name #9

Closed preliot closed 5 years ago

preliot commented 5 years ago

When i try to save a resource, it tries to save the wrong attribute name. How do i make a difference between the method client() that gets the attribute and client_id that needs to be saved?

In this example 'client' is a method on a orgizational level model. When saving this it should be client_id instead of client.

use App\Nova\Client\Client as ClientResource;
`return AjaxSelect::make(ClientResource::singularLabel(), 'client')
    ->get(config('nova.path') . '/orglevel/distributors/{distributor}/clients')
    ->parent('distributor')
    ->rules('required'); 
dillingham commented 5 years ago
AjaxSelect::make('Client', 'client_id')->onlyOnForms(),
BelongsTo::make('Client')->exceptOnForms(),
preliot commented 5 years ago

@dillingham Was stuck and it works now. Thanks!

My issue was that a belongsTo field needs the method on the model as a second argument to get the distributor.

\Laravel\Nova\Fields\BelongsTo::make('Distributor name', 'distributor', DistributorResource::class)

And AjaxSelect needs the attribute on the model:

return AjaxSelect::make('Client name', 'client_id') ->get(config('nova.path') . '/orglevel/distributors/{distributor}/clients') ->parent('distributor') ->rules('required');

For anyone else: Also note that the attribute in parent('attribute') must be identical to the attribute in get(config('nova.path') . '/orglevel/distributors/{attribute}/client))