Multiple Resources with different table query but tables appearance is not different
In my list I overridden getTableQuery() based on path
<?php
namespace App\Filament\Resources\BiciklResource\Pages;
use App\Filament\Resources\BiciklResource;
use App\Filament\Resources\BiciklAktivniResource;
use App\Filament\Resources\BiciklArhivaResource;
use App\Models\Bicikl;
use Filament\Pages\Actions;
use Filament\Resources\Pages\ListRecords;
use Illuminate\Database\Eloquent\Builder;
class ListBicikls extends ListRecords
{
protected static string $resource = BiciklResource::class;
protected function getActions(): array
{
return [
Actions\CreateAction::make(),
];
}
protected function getTableRecordsPerPageSelectOptions(): array
{
return [12, 24, 48, 96, -1];
}
protected function getTableQuery(): Builder
{
$path = request()->getPathInfo();
if ($path === '/admin/bicikli-aktivni') {
return parent::getTableQuery()->where('pCena', null);
} else if ( $path === '/admin/bicikli-arhiva' ) {
return parent::getTableQuery()->whereNotNull('pCena');
} else {
return parent::getTableQuery()->withoutGlobalScopes();
}
}
}
But, even I changed tables in my Resources, they all look like one in BiciklResource. That's probably becuase of this line protected static string $resource = BiciklResource::class;
Can I set this conditionally too or there is another way?
17 Replies
Please read the #✅┊rules about how to format your code properly.
Why aren't you creating separate Resources if you want separate table schema anyway?
Or if it's only the table: Create separate ListPages with their own URL, register them on the resources
getPages()
(before the view and edit route) and define a custom table schema and query on them.I want to achieve this, what is the best approach?
I created separate ListPages for active and archive with their own getTableQuery() , registered in getPages() and navigation items. Everything is wokring fine.
I tried to conditionally return tables in my table function based on path but as soon as I try to use pagination tables dissapear.
Can you please explain to me the last part you said: Define a custom table schema and query on them ?
Pretty much what you did. Define
getTableQuery
and getTableSchema
on separate ListPagesI can't find getTableSchema anywhere in documentation
Ok I used getTableColumns to define table. Did you mean that?
More than likely 🙂
I figured out everything 😄
Thank you!
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Please provide the code, usually that's because you have defined an action which finishes it's self without doing anyhting.
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
You need to provide more detail. Thats just a tableQuery not an action.
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Ok
You probably need to update your forUser() method if it's failing. But otherwise wher eis fine you can also do:
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Why are you doing the TableQuery? Why not just define the model in the resource?
Have default policies enabled so they can only access / view their own
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View