How to save one of my fields to a custom table? #1926
-
I have a table that is outside of the normal Laravel / Twill standard. It was a pre-existing table I had to import that contains data used by other applications. I do however also need to write a bit of data to it sometimes via twill. Specifically, I would write into a column of this table when another standard twill module is being saved. A value from a specific field (a multiselect field) in my twill form needs to save its content not into this other custom table. It just needs to save the selected items from the multi select field as a json value. How can I achieve this in twill ? Would I use the function afterSave($object, $fields) {} of the object's repository ? Any guidance would be really apprecaited. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I think that indeed the afterSave is the way to go. |
Beta Was this translation helpful? Give feedback.
-
Like @haringsrob said, // App\Repositories\YourRepository.php
public function afterSave($object, $fields)
{
// Skip this logic on create
if(!$object->wasRecentlyCreated){
// If you have a model for your table
\App\Models\YourModel::where('reference_id', $object->id)
->update(['json_column', $fields['multiselect_data']]);
// Or via DB facade
\DB::table('table')
->where('reference_id', $object->id)
->update(['json_column', $fields['multiselect_data']]);
}
return parent::afterSave($object, $fields);
} But this can be improved, it's up to you. |
Beta Was this translation helpful? Give feedback.
Like @haringsrob said,
afterSave
is a good place to make this operation. Eg.