Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

natchoum's avatar

modifiying database column and model field from an online form

Hi all. I've just started using Laravel ( v12 ) and it seems great.

I try to recreate an app i have done 15 years ago but with laravel. Since laravel is a MVC framework, i've created my models, my migration files, my controllers and my views and it was pretty easy but i come up with a problem :

There is a page on the admin dashboard which allow to add or remove columns from a database table. This is to allow the admin to add/remove some fields on a form and on the display listing page linked without the need to edit the php code and the db manually.

On the previous version of the app, i had created a text field in database which contains a json and i stored the datas there so it was dynamic. I could do this with an other DB than MySQL but it would mean migrate so much data from a base type to an other and it seems really long.

So i thought i could keep this solution but my problem is that with MVC approach, i would need to modify the model too ( the app is really old and i didn't know about MVC at this time so the queries are in the php file called by the request, without model or controller ).

I'm thinking about creating a model with a property which take the JSON value and explode it in the controller into an array from which i'll build the dynamic form and view but i think i'll loose the interest of having a model.

The other option that is see would be to create a script which modify the model and the controller files to add/remove the field and so having a model up to date even after a modification from the dashboard.

Is anybody has done something like this ? And if yes, have you a better way of doing this ?

0 likes
3 replies
Tray2's avatar

A JSON field in the database would work, and you would get a very dynamic way of creating keys, however you would have some issues with performance, when it comes to searching the values, and also you would add complexity to the application that is likely unwarranted.

I would rather create a regular table with a foreign key relation to whatever table the other data is stored, and then add two columns, one for key, and one for the value.

Something like this

$table->id();
$table->foreignIdFor(User::class)->contrained();
$table->string('key');
$table->string('value');
$table->timestamps();
natchoum's avatar

@Tray2 Thank you for your response, I must have been tired not to think about that ^^ Do we agree that i would have to create getter in my model to get data which return an array/object of data instead of returning an instance of the class ? So the User class ( in your example ) would not contain all the data in properties but only the fixed one and a property containing the dynamic data from the other table.

I'm a bit new to MVC as i stopped web development for a while so i'm asking myself if this would be correct or if i have to edit the model each time i add / remove a value in the linked table. It seems rather heavy and usually i don't like to create a script to edit others scripts if i can avoid it.

Thanks again

Tray2's avatar

@natchoum I would probably just do

User::where('id', $id)->with('related_data')->get();

Please or to participate in this conversation.