user10238849
user10238849

Reputation:

how to add column on table using query builder in laravel?

I want to add a column in my existing table in Laravel but not using migration. Is there any way to add column like this?

Upvotes: 2

Views: 3624

Answers (2)

user8661758
user8661758

Reputation:

DB::statement() function ---work for me 

DB::connection('mysql')->statement('ALTER TABLE table_name ADD column_name int   NULL DEFAULT(1)');

For all tables:

 foreach ($alltable as $key => $table)
 {
   DB::connection('mysql')->statement('ALTER TABLE '.$table1.' ADD store_id int   
   NULL DEFAULT(1)');
 }

Upvotes: 0

Rafael
Rafael

Reputation: 1525

You can use the DB::statement() function, from the DB Facade, according to their database documentation like this:

DB::statement('ALTER TABLE table_name ADD column_name datatype');

Though, it's not recommended.

The place for this things are in migrations for a reason, so you won't perform this more than once and you won't be executing creating or dropping columns dynamically (which shouldn't be).

So, use at your own risk.

Upvotes: 3

Related Questions