F
Filament11mo ago
Matthew

record in Create Record is null

According to the docs, this should output the record before it gets created, however it shows up null.
class CreateTest extends CreateRecord
{
protected static string $resource = TestResource::class;

protected function beforeCreate(): void
{
$record = $this->getRecord();
dd($record);
}
}
class CreateTest extends CreateRecord
{
protected static string $resource = TestResource::class;

protected function beforeCreate(): void
{
$record = $this->getRecord();
dd($record);
}
}
Any idea why?
6 Replies
Matthew
MatthewOP11mo ago
Is this a mistake? How can you have a record before you create? anyone?
Tobias Platen
Tobias Platen11mo ago
Looks to me like a copy error in the documentation. In the EditRecord class, the getRecord() method should, as documented, return the corresponding model. However, in the CreateRecord class, the getRecord() method will only return the model in the last lifecycle hook afterCreate(). Before that, you will receive null. To access the data before that, you will need to use $this->form->getState(). Do you have an example case in which the $this->halt() method can be correctly explained?
Matthew
MatthewOP11mo ago
Yes I do!
protected function beforeCreate(): void
{
$inputData = $this->data;
if (Carbon::createFromFormat('Y-m-d H:i:s', $inputData['deadline'], config('app.timezone'))->isPast()){
$this->halt();
}
}
protected function beforeCreate(): void
{
$inputData = $this->data;
if (Carbon::createFromFormat('Y-m-d H:i:s', $inputData['deadline'], config('app.timezone'))->isPast()){
$this->halt();
}
}
krekas
krekas11mo ago
think about it. how can $this->getRecord() be not null if record isn't created yet? can you post the link to docs
Tobias Platen
Tobias Platen11mo ago
This is more for validation purposes. The $this->halt() method should already be demonstrated with an example that cannot be easily resolved through validation alone.
krekas
krekas11mo ago
For validation purpose? That's what validation is for
Want results from more Discord servers?
Add your server