orlyapps / nova-belongsto-depend

Larave Nova BelongsTo Field with Dependcy
MIT License
182 stars 65 forks source link

missing viaResourceId #70

Open captainmio opened 4 years ago

captainmio commented 4 years ago

I'm having an error trying to execute the source code below in my resource file

public function fields(Request $request)
    {
        $userid = "";
        // pull hierarchy under this user's company

        $userid = $request->get('viaResourceId');

        $user_details = User::find($userid);
        $company_id = $user_details->company_id; 
        $options = [];
        $options = Hierarchy::where('company_id', '=', $company_id)->with('category')->get();

        return [
            ID::make()->sortable(),

            NovaBelongsToDepend::make('Hierarchy')
            ->options($options),

            NovaBelongsToDepend::make('Category')
            ->optionsResolve(function ($hierarchy) {
                // Reduce the amount of unnecessary data sent
                return $hierarchy->category()->get(['id', 'name']);
            })
            ->dependsOn('Hierarchy'),
        ];

    }

What i want is it will filter Category depending on the result in Hierarchy, The problem is when it tries to populate Category(executing another request in the backend part) $request->get('viaResourceId') becomes null and causes error. could anyone have a solution on this please?

figurluk commented 3 years ago

+1