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 Answers
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.
Comments
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)');
}