F
Filamentβ€’2w ago
frame

Custom resource page $this->record null when searching/sorting

The table renders fine initially, but when trying to sort or search $this->record is null. Am I missing some trait or something? πŸ€” Same question as this removed one https://discord.com/channels/883083792112300104/1301012727036121219
<?php

namespace App\Filament\Instructors\Resources\GroupResource\Pages;

use App\Filament\Instructors\Resources\GroupResource;
use App\Models\ExerciseResult;
use App\Models\Group;
use Filament\Actions\Concerns\InteractsWithRecord;
use Filament\Actions\Contracts\HasRecord;
use Filament\Resources\Pages\Page;
use Filament\Tables;
use Filament\Tables\Concerns\InteractsWithTable;
use Filament\Tables\Contracts\HasTable;
use Filament\Tables\Table;

class SimpleResults extends Page implements HasRecord, HasTable
{
use InteractsWithRecord;
use InteractsWithTable;

public function mount(string $record): void
{
$this->record = Group::where('uuid', $record)->firstOrFail();
}

protected static string $resource = GroupResource::class;

protected static string $view = 'filament.instructors.resources.group-resource.pages.simple-results';

public function getModel(): string
{
return Group::class;
}

public function table(Table $table)
{
return $table
->query(function () {
dump($this->record); // null if searching/sorting

return ExerciseResult::forGroup($this->record);
})
->columns([
Tables\Columns\TextColumn::make('created_at')
->label('Created at')
->sortable()
->searchable(),
])
->actions([]);
}
}
<?php

namespace App\Filament\Instructors\Resources\GroupResource\Pages;

use App\Filament\Instructors\Resources\GroupResource;
use App\Models\ExerciseResult;
use App\Models\Group;
use Filament\Actions\Concerns\InteractsWithRecord;
use Filament\Actions\Contracts\HasRecord;
use Filament\Resources\Pages\Page;
use Filament\Tables;
use Filament\Tables\Concerns\InteractsWithTable;
use Filament\Tables\Contracts\HasTable;
use Filament\Tables\Table;

class SimpleResults extends Page implements HasRecord, HasTable
{
use InteractsWithRecord;
use InteractsWithTable;

public function mount(string $record): void
{
$this->record = Group::where('uuid', $record)->firstOrFail();
}

protected static string $resource = GroupResource::class;

protected static string $view = 'filament.instructors.resources.group-resource.pages.simple-results';

public function getModel(): string
{
return Group::class;
}

public function table(Table $table)
{
return $table
->query(function () {
dump($this->record); // null if searching/sorting

return ExerciseResult::forGroup($this->record);
})
->columns([
Tables\Columns\TextColumn::make('created_at')
->label('Created at')
->sortable()
->searchable(),
])
->actions([]);
}
}
Solution:
Actually did not work I just messed up. What really worked is setting additional public MyModel $myRecord; in the page and setting that on mount and using that instead of $this->record
Jump to solution
2 Replies
frame
frameOPβ€’2w ago
resolveRecord needs to be defined, then it works πŸ‘
public function mount(string $record): void
{
$this->record = $this->resolveRecord($record);
}

public function resolveRecord($record)
{
return Group::where('uuid', $record)->firstOrFail();
}
public function mount(string $record): void
{
$this->record = $this->resolveRecord($record);
}

public function resolveRecord($record)
{
return Group::where('uuid', $record)->firstOrFail();
}
Solution
frame
frameβ€’2w ago
Actually did not work I just messed up. What really worked is setting additional public MyModel $myRecord; in the page and setting that on mount and using that instead of $this->record

Did you find this page helpful?