Insert data into 2 different tables from a single form using OctoberCMS Builder Plugin
I am using OctoberCMS builder plugin to create forms to store data into database. By default, each model belongs to a single database table. And a form can store the data into a single database table. So how to Insert data into 2 different tables captured from a single form using Builder Plugin.
Regards
Answer
To do that You'll have to handle the saving process manually using the model class.
1- Open your model class.
2- Write a model even function like public function afterSave(){}
For example if I have a total
field in reservation
model and I wanna this field value to be inserted automatically in another table called accounting
.
public function afterSave(){
$accounting = \Namespace\Pluginname\Models\Accounting::find(1);
$accounting->myField = $this->total;
$accounting->save();
}
So within afterSave
, beforeSave
, beforeCreate
or afterCreate
you can handle the saving process of the models.
To see the differences between those functions you can read more on this link: https://octobercms.com/docs/database/model#events
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → I can't do a foreign key, constraint error
- → Setting a default value on settings form return null in Octobercms
- → Eloquent Multitable query
- → "Laravel 5.1" add user and project with userId
- → Image does not go in database with file name only tmp name saved?
- → Database backup with custom code in laravel 5 and get the data upto 10 rows from per table in database
- → Trait 'IlluminateFoundationBusDispatchesJobs' not found
- → Setting the maxlength of text in an element that is displayed
- → laravel check between 2 integer from database
- → how to retrieve image from database in laravel 5.1?
- → relationship for database Column type object
- → Carousel in Laravel 4 does not show as expected