Home > OS >  add new column when user wants to - laravel
add new column when user wants to - laravel

Time:06-04

how can I add a new column with custom name whenever user wants to do it? It's like user is on some site and enters a column name, for example "cars" and then clicks on button "add new column" which should run the action that adds a new column named "cars" to existing table.

I know it can be done with migrations but I have to manually fill up the up() and down() functions but I want it to be done automatically without a need to write something in these functions. Is it possible?

CodePudding user response:

First of all it is not good idea to create column from user input. You can create custom field structure for this purpose. But let me answer your question first.

You can run below code in your controller. Migrations just runs this commands nothing complicated.

Schema::table('users', function($table) {
  $table->integer('car_id')->nullable();
});

You can search custom fields for Laravel. Im sure that there are multiple packages for this purpose.

CodePudding user response:

You should try to create a json column that will repertory all new columns with their data in your database, or change your SQL to Document data, probably the best for your use case since you want to allow your user to create new attributs. :)

  • Related