F
Filament12mo ago
sundays

Calculated fields in form builder

Hi, I can't find the documentation section that describes how to do "calculated" fields in the form builder. I have a FileUpload field and I need to store some information about the file in the database, in my case audio length in milliseconds. What's the lifecycle function I can use to fill my audio_length field based on the file upload? Do I have to do this by listening to a model created event, and fill the column after the row is added?
2 Replies
sundays
sundaysOP12mo ago
I came across afterStateUpdated, but I only need to get the file's size once, when the form is being submitted. Is this still the right way to do it?
Pede
Pede12mo ago
You can do it in your model with the following code:
public static function boot()
{
parent::boot();

self::creating(function($model){
// ---- add your code to calculate filesize ----

//Set attributes filesize to be the calculated value
$model->attributes['filesize'] = 954;
});
}
public static function boot()
{
parent::boot();

self::creating(function($model){
// ---- add your code to calculate filesize ----

//Set attributes filesize to be the calculated value
$model->attributes['filesize'] = 954;
});
}
Want results from more Discord servers?
Add your server