2

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?

2
  • Through database tools such as PHPMyAdmin / SQL Yog etc. etc. Commented Aug 28, 2018 at 8:58
  • But then you'd break your migration which I wouldn't recommend. Commented Aug 28, 2018 at 8:58

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0
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)');
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.