F
Filament4mo ago
BenRay

getEloquentQuery() is not respected when using Live()

In a resource page, after live() is triggered, the page is reloaded with the wrong model query. How do I keep the query the same after live()? Image 1 is on initial page load (respects getEloquentQuery()). Image 2 is after live().
No description
No description
8 Replies
Dennis Koch
Dennis Koch4mo ago
Can you share some code?
BenRay
BenRayOP4mo ago
Hi Dennis, here is the small amount of code needed to reproduce this issue. This is on a fresh installation of filament 3 on php 8.3.
<?php

namespace App\Filament\Resources;

use App\Filament\Resources\EmployeeResource\Pages;
use App\Models\Employee;
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\Placeholder;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables\Actions\BulkActionGroup;
use Filament\Tables\Actions\DeleteAction;
use Filament\Tables\Actions\DeleteBulkAction;
use Filament\Tables\Actions\EditAction;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;

class EmployeeResource extends Resource
{
protected static ?string $model = Employee::class;

protected static ?string $slug = 'employees';

protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

public static function form(Form $form): Form
{
return $form
->schema([
Checkbox::make('live')
->live()
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
])
->filters([
//
])
->actions([
EditAction::make(),
DeleteAction::make(),
])
->bulkActions([
BulkActionGroup::make([
DeleteBulkAction::make(),
]),
]);
}

public static function getPages(): array
{
return [
'index' => Pages\ListEmployees::route('/'),
'create' => Pages\CreateEmployee::route('/create'),
'edit' => Pages\EditEmployee::route('/{record}/edit'),
];
}

public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()->select('id', 'name');
}
}
<?php

namespace App\Filament\Resources;

use App\Filament\Resources\EmployeeResource\Pages;
use App\Models\Employee;
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\Placeholder;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables\Actions\BulkActionGroup;
use Filament\Tables\Actions\DeleteAction;
use Filament\Tables\Actions\DeleteBulkAction;
use Filament\Tables\Actions\EditAction;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;

class EmployeeResource extends Resource
{
protected static ?string $model = Employee::class;

protected static ?string $slug = 'employees';

protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

public static function form(Form $form): Form
{
return $form
->schema([
Checkbox::make('live')
->live()
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
])
->filters([
//
])
->actions([
EditAction::make(),
DeleteAction::make(),
])
->bulkActions([
BulkActionGroup::make([
DeleteBulkAction::make(),
]),
]);
}

public static function getPages(): array
{
return [
'index' => Pages\ListEmployees::route('/'),
'create' => Pages\CreateEmployee::route('/create'),
'edit' => Pages\EditEmployee::route('/{record}/edit'),
];
}

public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()->select('id', 'name');
}
}
<?php

namespace App\Filament\Resources\EmployeeResource\Pages;

use App\Filament\Resources\EmployeeResource;
use Filament\Actions\DeleteAction;
use Filament\Resources\Pages\EditRecord;

class EditEmployee extends EditRecord
{
protected static string $resource = EmployeeResource::class;

protected function getHeaderActions(): array
{
return [
DeleteAction::make(),
];
}
}
<?php

namespace App\Filament\Resources\EmployeeResource\Pages;

use App\Filament\Resources\EmployeeResource;
use Filament\Actions\DeleteAction;
use Filament\Resources\Pages\EditRecord;

class EditEmployee extends EditRecord
{
protected static string $resource = EmployeeResource::class;

protected function getHeaderActions(): array
{
return [
DeleteAction::make(),
];
}
}
BenRay
BenRayOP4mo ago
Here is the query on page load (respecting geteloquentquery())
No description
BenRay
BenRayOP4mo ago
here is the query on page refresh after the live checkbox is clicked
No description
toeknee
toeknee4mo ago
Why are you doing that: return parent::getEloquentQuery()->select('id', 'name'); The reason it changes is likely because you haven't defined the columns and the selects are removed if the columns no longer exist. If you add the columns but add a where into the eloquent query does it respect the additional where? Also your live is on a form and the form isn't really part of the eloquent query, since the eloquent query should be run but be part of the table.
BenRay
BenRayOP4mo ago
I'm using ->select because the database table I'm working with has a lot of columns and I would like to reduce the size of the query if possible. It does respect the ->where, interestingly enough. Why does it not keep the ->select? The columns are defined on the SQL side but is there another place I need to define them?
toeknee
toeknee4mo ago
So you can replace the query by using ->query(fn($query) => $query->select()) on the table it's self which removes the eloquent query function. Does that stay as expected? I suspect the query is replaced in the table because of column selection by default
BenRay
BenRayOP4mo ago
->query(fn($query) => $query->select()) results in
An attempt was made to evaluate a closure for [Filament\Tables\Table], but [$query] was unresolvable.
An attempt was made to evaluate a closure for [Filament\Tables\Table], but [$query] was unresolvable.
I tried this with ->modifyQueryUsing() and it modified the query for the table fine, but not the form. It says here that using getEloquentQuery() should apply to the entire resource, though. Which it does...until live() is triggered
No description

Did you find this page helpful?