Skip to content

Commit

Permalink
Merge pull request #63 from iRaziul/main
Browse files Browse the repository at this point in the history
Optimize and reduce db queries
  • Loading branch information
datlechin authored Dec 27, 2024
2 parents 3c01d27 + fcec82c commit 4f56bb8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
9 changes: 8 additions & 1 deletion src/Concerns/HasLocationAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

trait HasLocationAction
{
protected ?Collection $menus = null;

protected ?Collection $menuLocations = null;

public function getLocationAction(): Action
Expand Down Expand Up @@ -70,7 +72,7 @@ public function getLocationAction(): Action
->label(__('filament-menu-builder::menu-builder.actions.locations.form.menu.label'))
->searchable()
->hiddenLabel($key !== $this->getRegisteredLocations()->keys()->first())
->options($this->getModel()::all()->pluck('name', 'id')->all()),
->options($this->getMenus()->pluck('name', 'id')->all()),
]),
)->all() ?: [
Components\View::make('filament-tables::components.empty-state.index')
Expand All @@ -81,6 +83,11 @@ public function getLocationAction(): Action
]);
}

protected function getMenus(): Collection
{
return $this->menus ??= FilamentMenuBuilderPlugin::get()->getMenuModel()::all();
}

protected function getMenuLocations(): Collection
{
return $this->menuLocations ??= FilamentMenuBuilderPlugin::get()->getMenuLocationModel()::all();
Expand Down
9 changes: 5 additions & 4 deletions src/Models/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ public function menuItems(): HasMany

public static function location(string $location): ?self
{
return FilamentMenuBuilderPlugin::get()
->getMenuLocationModel()::with(['menu' => fn (Builder $query) => $query->where('is_visible', true)])
->where('location', $location)
->first()?->menu;
return self::query()
->where('is_visible', true)
->whereRelation('locations', 'location', $location)
->with('menuItems')
->first();
}
}

0 comments on commit 4f56bb8

Please sign in to comment.