Код: Выделить всё
Section::make('Game control')
->schema([
Forms\Components\Placeholder::make('status')
->label('Current status')
->content(function ($record) {
if (!$record->is_running && !$record->current_round && !$record->is_finished) {
return 'The game has not started yet.';
} elseif ($record->is_finished && !$record->is_running) {
return 'Game finished.';
} else {
return 'Current round: ' . $record->currentRound->sort;
}
}),
Forms\Components\Actions::make([
Forms\Components\Actions\Action::make('startGame')
->label('Start game')
->button()
->visible(fn($record) => !$record->is_running && !$record->is_finished)
->action(function (Game $record, EditRecord $livewire) {
$record->startGame();
$livewire->refreshFormData(['is_running', 'is_finished', 'current_round']);
}),
Forms\Components\Actions\Action::make('startNextRound')
->label('Next Round')
->button()
->visible(fn($record) => $record->is_running && !$record->isLastRound())
->action(function (Game $record, EditRecord $livewire) {
$record->startNextRound();
$livewire->refreshFormData(['is_running', 'is_finished', 'current_round']);
}),
Forms\Components\Actions\Action::make('finishGame')
->label('Show results')
->button()
->visible(fn($record) => $record->is_running && $record->isLastRound())
->action(function (Game $record, EditRecord $livewire) {
$record->finishGame();
$livewire->refreshFormData(['is_running', 'is_finished', 'current_round']);
})
]),
])
->visibleOn('edit')
Код: Выделить всё
class Game extends Model {
public function isLastRound() {
return $this->current_round // check if current round is set
&& $this->rounds()->count() === $this->currentRound->sort; // check if current round is the last round
}
public function startGame() {
$this->is_running = true;
$this->current_round = $this->rounds()->orderBy('sort')->first()->id;
$this->save();
}
public function startNextRound() {
$currentRoundSort = $this->rounds()->where('id', $this->current_round)->first()->sort;
$nextRound = $this->rounds()->where('sort', $currentRoundSort + 1)->first();
$this->current_round = $nextRound->id;
$this->save();
}
public function finishGame() {
$this->is_running = false;
$this->is_finished = true;
$this->save();
}
}
Как мне добиться автоматического обновления страницы, раздела или всей формы?
Я наткнулся на EditRecord::refreshFormData(), но не смог этого сделать заставить его работать правильно: как ни странно, текущее решение работает для начала и завершения игры, но не для следующего раунда.
Подробнее здесь: https://stackoverflow.com/questions/791 ... n-filament
Мобильная версия