Grace
Grace

Reputation: 299

laravel add and delete column to database using controller

is it possible to add and delete column to my existing database using the controller? is it possible not to use the migration? and how do my model automatically picked up the new column which is create and automatically put in inside fillable? anyone has idea on how to approach this type of situation,if you could point me into a tutorial that would be so cool.

Reason: i have a table with the student mark-book points breakdown column example: [Exam, Homework,Quiz etc..] then every term or year we will remove it or changed it or add more so that's why i need to something like dynamic approach on this matter. where anytime i can change the column or add new column.

Upvotes: 1

Views: 2462

Answers (1)

Brian Lee
Brian Lee

Reputation: 18197

Same way the migrations do it, use the Schema builder class. For example:

$newColumnType = 'string';
$newColumnName = 'my_new_column';

Schema::table('my_table', function (Blueprint $table) use ($newColumnType, $newColumnName) {
    $table->$newColumnType($newColumnName);
});

You probably should use $guarded = ['id', 'foo', 'bar'] in your model instead of fillable if you're going to be adding columns.

Upvotes: 3

Related Questions