Unable to save hidden() fields to database !!

Hi,
Step::make('Etap 7')
->description("Montant Octroyé")
->schema([
Fieldset::make('Montant Octroyé')
->relationship('granted_amount')
->schema([

TextInput::make('granted_amount')
->label('Montant Octroyé'),


TextInput::make('current_year')
->label('current_year'),


TextInput::make('annee_inscription')
->label("Année d'inscription")->hiddenn()
->default(date("Y") . "/" . date("Y") + 1),

]),
]),
Step::make('Etap 7')
->description("Montant Octroyé")
->schema([
Fieldset::make('Montant Octroyé')
->relationship('granted_amount')
->schema([

TextInput::make('granted_amount')
->label('Montant Octroyé'),


TextInput::make('current_year')
->label('current_year'),


TextInput::make('annee_inscription')
->label("Année d'inscription")->hiddenn()
->default(date("Y") . "/" . date("Y") + 1),

]),
]),
And when I debug
protected function afterCreate(): void
{
dd("After", $this->data);

}
protected function afterCreate(): void
{
dd("After", $this->data);

}
The data is displayed
"granted_amount" => array:3 [
"granted_amount" => null
"current_year" => null
"annee_inscription" => "2023/2024"
]
"granted_amount" => array:3 [
"granted_amount" => null
"current_year" => null
"annee_inscription" => "2023/2024"
]
Do you have any idea? Thanks.
Solution:
Okay , thank you, i will try it
Jump to solution
6 Replies
Dennis Koch
Dennis Koch15mo ago
Yes. That is expected. Hidden fields are not saved. Use mutateDataBeforeSave() or similar form hooks to add your data.
ROOT-LEE
ROOT-LEEOP15mo ago
Thank you Koch, Since I'm on the Create page, I used mutateFormDataBeforeCreate(), knowing that I already use beforeCreate() method, I'm not entirely aware of the distinction between them. So by hiding this step(), the data is not saved to the database even using one of these two methods, but when I debug afterCreate() method, I see that the data is populated?? 🤔
Dennis Koch
Dennis Koch15mo ago
Yes. It's not about the step but the hidden field. Saving logic is using $this->form->getState() which doesn't return hidden fields. You shouldn't rely on hidden fields anyway. They could be modified by the user. If you want to set a default you can set it in ->afterCreate() or wherever you want to modify the data.
ROOT-LEE
ROOT-LEEOP15mo ago
yes, I agree with you , perfect, so using standard Laravel code to insert data into database ( Fantastic! )
protected function afterCreate(): void
{
// Can I get the current inserted id of this model: Doctorant student?
$doctorant=Doctorant::select('id')->where('code_inscription',$this->data['code_inscription'])
->first()['id'];
if ($doctorant){

//getting data from current instance
$annee_inscription = $this->data['annee_inscription'];
$current_year = $this->data['these']['current_year'];


$montantOctroye = new MontantOctroye();

$montantOctroye->doctorant_id = $doctorant;
$montantOctroye->current_year = $current_year;
$montantOctroye->annee_inscription = $annee_inscription;
$montantOctroye->save();

}
protected function afterCreate(): void
{
// Can I get the current inserted id of this model: Doctorant student?
$doctorant=Doctorant::select('id')->where('code_inscription',$this->data['code_inscription'])
->first()['id'];
if ($doctorant){

//getting data from current instance
$annee_inscription = $this->data['annee_inscription'];
$current_year = $this->data['these']['current_year'];


$montantOctroye = new MontantOctroye();

$montantOctroye->doctorant_id = $doctorant;
$montantOctroye->current_year = $current_year;
$montantOctroye->annee_inscription = $annee_inscription;
$montantOctroye->save();

}
Is there a way to retrieve the inserted id of this instance? Thank you very much for your help🤝 " you can set it in ->afterCreate() or wherever you want to modify the data." was the key
Dennis Koch
Dennis Koch15mo ago
$this->record should contain the record. Or it’s passed as the argument of the method. Not sure.
Solution
ROOT-LEE
ROOT-LEE15mo ago
Okay , thank you, i will try it
Want results from more Discord servers?
Add your server