Is it possible to add "Previous" and "Next" buttons on Form ?
Hello, I want buttons to go to previous record or next record when I am editing one ?
Is it possible ?
Is it possible ?
Solution
This will do what you're asking
class EditTask extends EditRecord
{
protected static string $resource = TaskResource::class;
protected function getHeaderActions(): array
{
$nextRecord = $this->getNewRecord(true);
$prevRecord = $this->getNewRecord(false);
return [
DeleteAction::make(),
Action::make('next')
->url($nextRecord ? TaskResource::getUrl('edit', ['record' => $nextRecord->getKey()]) : '#')
->disabled(!$nextRecord),
Action::make('previous')
->url($prevRecord ? TaskResource::getUrl('edit', ['record' => $prevRecord->getKey()]) : '#')
->disabled(!$prevRecord),
];
}
protected function getNewRecord(bool $nextRecord){
$currentRecord = $this->getRecord();
if (!$currentRecord) {
return null;
}
$record = null;
if ($nextRecord){
$record = $currentRecord->where('id', '>', $currentRecord->getKey());
}else{
$record = $currentRecord->where('id', '<', $currentRecord->getKey())->orderBy('id', 'desc');
}
return $record->first();
}
}class EditTask extends EditRecord
{
protected static string $resource = TaskResource::class;
protected function getHeaderActions(): array
{
$nextRecord = $this->getNewRecord(true);
$prevRecord = $this->getNewRecord(false);
return [
DeleteAction::make(),
Action::make('next')
->url($nextRecord ? TaskResource::getUrl('edit', ['record' => $nextRecord->getKey()]) : '#')
->disabled(!$nextRecord),
Action::make('previous')
->url($prevRecord ? TaskResource::getUrl('edit', ['record' => $prevRecord->getKey()]) : '#')
->disabled(!$prevRecord),
];
}
protected function getNewRecord(bool $nextRecord){
$currentRecord = $this->getRecord();
if (!$currentRecord) {
return null;
}
$record = null;
if ($nextRecord){
$record = $currentRecord->where('id', '>', $currentRecord->getKey());
}else{
$record = $currentRecord->where('id', '<', $currentRecord->getKey())->orderBy('id', 'desc');
}
return $record->first();
}
}