Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Laravel Nova v4 support #113

Open
figurluk opened this issue Apr 8, 2022 · 6 comments
Open

Laravel Nova v4 support #113

figurluk opened this issue Apr 8, 2022 · 6 comments

Comments

@figurluk
Copy link

figurluk commented Apr 8, 2022

No description provided.

@orlyapps
Copy link
Owner

dependsOn is now built into nova 4 and you can do this.
This package will no longer be maintained for nova 4.

public function fields(NovaRequest $request)
    {
        return [
            BelongsTo::make('Unternehmen', 'company', Company::class)
                ->filterable()
                ->sortable(),
            BelongsTo::make('Bereich', 'department', Department::class)
                ->nullable()
                ->exceptOnForms()
                ->sortable(),
            Select::make('Bereich', 'department_id')
                ->onlyOnForms()
                ->dependsOn('company', function(Select $field, NovaRequest $request, FormData $formData) {
                    $company = \App\Company::where('id', $formData->company ?? $this->company_id)->first();
                    $field->options($company?->departments()->pluck('name', 'id'));
                })
                ->default($this->location?->id)
                ->nullable()
                ->onlyOnForms(),
            BelongsTo::make('Niederlassung', 'location', Location::class)
                ->nullable()
                ->sortable()
                ->exceptOnForms()
                ->withoutTrashed(),
            Select::make('Niederlassung', 'location_id')
                ->onlyOnForms()
                ->dependsOn('company', function(Select $field, NovaRequest $request, FormData $formData) {
                    $company = \App\Company::where('id', $formData->company ?? $this->company_id)->first();
                    $field->options($company?->locations()->pluck('name', 'id'));
                })
                ->default($this->department?->id)
                ->nullable()
                ->onlyOnForms(),
        ];
    }

@ch314srun
Copy link

ch314srun commented May 23, 2022

@orlyapps how about filterable with dependsOn?

@joserick
Copy link

I found a repository that compacts this process: https://github.com/webparking/nova-belongs-to-dependency

@orlyapps
Copy link
Owner

A better example (relatableQueryUsing):

            BelongsTo::make('Unternehmen', 'company', Company::class)->rules('required'),
            BelongsTo::make('Bereich', 'department', Department::class)
                ->dependsOn(['company'], function (BelongsTo $field, NovaRequest $request, FormData $data) {
                    $field->relatableQueryUsing(function ($request, $query) use ($data) {
                        return $query->where('company_id', $data->company);
                    });
                })
                ->nullable(),
            BelongsTo::make('Niederlassung', 'location', Location::class)
                ->dependsOn(['company'], function (BelongsTo $field, NovaRequest $request, FormData $data) {
                    $field->relatableQueryUsing(function ($request, $query) use ($data) {
                        return $query->where('company_id', $data->company);
                    });
                })
                ->nullable(),
            BelongsTo::make('Standort', 'place', Place::class)
                ->dependsOn(['location'], function (BelongsTo $field, NovaRequest $request, FormData $data) {
                    $field->relatableQueryUsing(function ($request, $query) use ($data) {
                        return $query->where('location_id', $data->location);
                    });
                })
                ->nullable(),

@prookie
Copy link

prookie commented Feb 27, 2023

When was relatableQueryUsing introduced? My Nova app is still on 4.13.0 because every Nova update breaks stuff. relatableQueryUsing seems to be missing there. Right now I can not invest all those hours for testing after performing an update.

@ryanrapini
Copy link


use Illuminate\Database\Eloquent\Builder;
use Laravel\Nova\Fields\FormData;

...

BelongsTo::make('Owner', 'user', User::class)->dependsOn('organization', function(BelongsTo $field, NovaRequest $request, FormData $formData) {
     $field->relatableQueryUsing(function (NovaRequest $request, Builder $query) use ($formData) {
        $query->when($formData->organization, function ($query) use ($formData) {
            $query->where('organization_id', $formData->organization);
        });
    });
})->nullable(),

Chiming in with a complete solution in Nova 4, very close to @orlyapps but I use when() which is necessary for my use case. Included imports to save time iterating, since I always forget those.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants