nova-belongsto-depend icon indicating copy to clipboard operation
nova-belongsto-depend copied to clipboard

Multiple Resources using the Same Model, breaking with NovaBelongsToDepend field

Open owen-soak opened this issue 3 years ago • 2 comments

I have encountered some odd stuff happening, i have have two resources both using the same model and extend the sme base Resource class.

Resources:

\App\Nova\ReportTestAppointments \App\Nova\TestAppointments

Model:

public static $model = \App\TestAppointment::class;

Within \App\Nova\TestAppointments i have a fields as below.

NovaBelongsToDepend::make('Instructor', 'instructor', User::class) ->placeholder('Choose an instructor') ->optionsResolve(function ($booking) { return \App\User::instructor()->ofBranch($booking->branch_id)->get(['id', 'name']); }) ->rules(new InstructorRequiredRule) ->nullable() // necessary to accept null value ->dependsOn('Booking') ->readonly(function ($request) { return !$request->user()->can(Permissions::TEST_UPDATE_INSTRUCTOR) && !$request->user()->isSuperAdmin(); }),

NovaBelongsToDepend::make('Booking') ->options(\App\Booking::all()) ->readonly(), `

Now i'm getting the below error when i try and access the \App\Nova\TestAppointments resource.

*Can not find the Field "instructor" in the Model "App\Nova\ReportTestAppointments*

I can only assume something is getting confused here as each resource are unrelated to each other beyond the model.

owen-soak avatar Dec 18 '20 17:12 owen-soak

If anyone has an opportunity to provide insight into the above its much appreciated :)

owen-soak avatar Feb 09 '21 14:02 owen-soak

Probably the formatting is slightly better like that:

NovaBelongsToDepend::make('Instructor', 'instructor', User::class) 
->placeholder('Choose an instructor') 
->optionsResolve(function ($booking) { 
    return \App\User::instructor()->ofBranch($booking->branch_id)->get(['id', 'name']);
 })
->rules(new InstructorRequiredRule)
->nullable() // necessary to accept null value 
->dependsOn('Booking') 
->readonly(function ($request) { 
    return !$request->user()->can(Permissions::TEST_UPDATE_INSTRUCTOR) && !$request->user()->isSuperAdmin();
 }),

NovaBelongsToDepend::make('Booking')
->options(\App\Booking::all())
->readonly(),
...

I suppose you need to create the relationship field (belongsTo or hasMany) in the model

MeerKatDev avatar Mar 29 '21 13:03 MeerKatDev