List Resources - GetTabs: modifyQueryUsing not working with relationships or scopes

Hi everyone, I'm working on setting up multiple tabs in a List for a particular resource. The idea is to have each tab display Companies, filtered by their type. I've included a code snippet for your reference.
<?php

namespace App\Filament\Admin\Resources\CompanyResource\Pages;

use App\Filament\Admin\Resources\CompanyResource;
use Filament\Actions\CreateAction;
use Filament\Resources\Pages\ListRecords;
use Illuminate\Database\Eloquent\Builder;

class ListCompanies extends ListRecords
{
protected static string $resource = CompanyResource::class;

public function getTabs(): array
{
return [
'manufacturers' => ListRecords\Tab::make('manufacturers')
->modifyQueryUsing(fn(Builder $builder) => $builder->onlyManufacturers()),
'carriers' => ListRecords\Tab::make('carriers')
->modifyQueryUsing(fn(Builder $builder) => $builder->onlyCarriers()),
'suppliers' => ListRecords\Tab::make('suppliers')
->modifyQueryUsing(fn(Builder $builder) => $builder->onlySuppliers()),
];
}
}
<?php

namespace App\Filament\Admin\Resources\CompanyResource\Pages;

use App\Filament\Admin\Resources\CompanyResource;
use Filament\Actions\CreateAction;
use Filament\Resources\Pages\ListRecords;
use Illuminate\Database\Eloquent\Builder;

class ListCompanies extends ListRecords
{
protected static string $resource = CompanyResource::class;

public function getTabs(): array
{
return [
'manufacturers' => ListRecords\Tab::make('manufacturers')
->modifyQueryUsing(fn(Builder $builder) => $builder->onlyManufacturers()),
'carriers' => ListRecords\Tab::make('carriers')
->modifyQueryUsing(fn(Builder $builder) => $builder->onlyCarriers()),
'suppliers' => ListRecords\Tab::make('suppliers')
->modifyQueryUsing(fn(Builder $builder) => $builder->onlySuppliers()),
];
}
}
When I navigate to the resource list, I run into an error mentioning an undefined method called onlyManufacturers, which is actually a scope in my model. I've also tried running the query without using the scope, but I'm still encountering issues. I've attached a screenshot of the dd($builder) output as well, just in case it's helpful. Is it possible that I'm missing something here, or could there be something off in the documentation? Thanks in advance for your help!
No description
No description
Solution:
Try
->modifyQueryUsing(fn (Builder $query) => $query->yourScope(),
->modifyQueryUsing(fn (Builder $query) => $query->yourScope(),
. I think the injection should be $query. I could easily be wrong...
Jump to solution
5 Replies
Solution
Mark Chaney
Mark Chaney15mo ago
Try
->modifyQueryUsing(fn (Builder $query) => $query->yourScope(),
->modifyQueryUsing(fn (Builder $query) => $query->yourScope(),
. I think the injection should be $query. I could easily be wrong
cheesegrits
cheesegrits15mo ago
Also, as per #✅┊rules ... don't send screenshots of code. Some of us are old farts and we can't read code screenshots. So if you haven't done so already, you should definitely read the rules.
When your code example is short please format it using backticks. Use GitHub Gists for long code examples. Don't send screenshots of code.
Eugen Pașca
Eugen PașcaOP15mo ago
Thank you for pointing it out. My bad, I redacted the original question.
cheesegrits
cheesegrits15mo ago
Thanks!
Want results from more Discord servers?
Add your server