Custom page with one edit form
I have created a custom page with a modification form. The form is working properly (The data is modified in the database).
My only question is, is the implementation of the data saving and query function this way correct?
Thanks for the help.
PageSettings.php
<?php
namespace App\Filament\Pages;
use App\Services\AppointmentService;
use App\Models\Setting;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Pages\Page;
use Filament\Actions\Action;
use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Support\Exceptions\Halt;
use Filament\Notifications\Notification;
use Illuminate\Database\Eloquent\Model;
class PageSettings extends Page implements HasForms
{
use InteractsWithForms;
protected static ?string $navigationIcon = 'heroicon-o-document-text';
protected static string $view = 'filament.pages.page-settings';
public ?array $data = [];
public function mount(): void
{
$this->form->fill($this->getSetting());
}
public function form(Form $form): Form
{
return $form
->schema([])
->statePath('data');
}
protected function getFormActions() { ... }
//data saving
public function save(): void
{
try {
$data = $this->form->getState();
$this->handleRecordUpdate($data);
} catch (Halt $exception) {
return;
}
Notification::make()
->success()
->title(__('Successfully data updated'))
->send();
}
// data query to mount() function
protected function getSetting(): array
{
$setting = Setting::first();
return $setting->attributesToArray();
}
// data query from database
protected function handleRecordUpdate(array $data): Model
{
$record = Setting::find(1);
$record->update($data);
return $record;
}
}
0 Replies