orlyapps / nova-belongsto-depend

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

Use a local scope for main filter does not work #77

Closed xoco70 closed 3 years ago

xoco70 commented 3 years ago

Hi,

I have 2 dependants fields: Company and Operations. An Operation belongsTo a Company.

I also have 2 user roles, SuperUser, and SimpleUser. SuperUser can see all companies, while simpleUser belongsTo a company

So, I have a local scope in Company model:

/**
     * @param Builder $query
     * @return Builder
     */
    public function scopeMines($query)
    {
        if (auth()->user()->isSuperAdmin()) {
            return $query;
        }

        return $query->where('company_id', auth()->user()->company_id);
    }

I use it in Resource:

NovaBelongsToDepend::make('Structure', 'company', \App\Nova\Company::class)
            ->options(\App\Models\Company::mines())
            ->rules('required');

I also tried to put the query directly in ->options() but got no luck:

$structure = NovaBelongsToDepend::make('Structure', 'company', \App\Nova\Company::class)
            ->options(function () {
                return \App\Models\Company::all(); // gives empty list
            })
            ->rules('required');

But it gives me a empty list.

A better thing for me should be to completely hide it, with ->hideWhenCreating()->hideWhenUpdating() but then I can't pass the value of company to child select

xoco70 commented 3 years ago

My bad, had to use ->get()

NovaBelongsToDepend::make('Structure', 'company', \App\Nova\Company::class)
            ->options(\App\Models\Company::mines()->get())
            ->rules('required');