Insert Data Into 2 Different Tables From A Single Form Using OctoberCMS Builder Plugin

- 1 answer

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.




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;

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: