0

I'm new to laravel framework. For making a blog URL's to SEO friendly, I need to add an extra column to the existing blog tables for the laravel website. Can we directly add a column to a table directly in the database or not? Can we add a column without commands or migrations? Would you please suggest an easy method to add the column?

sɐunıɔןɐqɐp
  • 3,332
  • 15
  • 36
  • 40
Udaya
  • 11
  • 2
  • 6
  • you can use migration as shown in [this answer](https://stackoverflow.com/a/16791988/4648586), or add it directly using the dbms. may we know what dbms you are using? – Bagus Tesa Oct 25 '19 at 07:04
  • thanks for the suggestion. we are using mysql – Udaya Oct 25 '19 at 07:37
  • if you are using raw mysql without laravel migrations, you can use [`ALTER TABLE`](https://dev.mysql.com/doc/refman/8.0/en/alter-table.html) query, something like `ALTER TABLE ADD COLUMN "my-new-column" varchar(MAX)` or something... – Bagus Tesa Oct 25 '19 at 07:51
  • Best solution is to use a framework the way they intent you to use it. I would strongly suggest using laravel's migrations. – killstreet Oct 25 '19 at 12:40

2 Answers2

3

Add migration

php artisan make:migration add_fieldname_to_tablename

Code methods migration

public function up()
{
        Schema::table('tablename', function (Blueprint $table) {
            $table->datatype('column_name')->nullable();
        });
}    

public function down()
{
        Schema::table('tablename', function (Blueprint $table) {
            $table->dropColumn('column_name');
        });
}

Run migration

php artisan migrate
mitesh
  • 147
  • 1
  • 9
Nick
  • 536
  • 2
  • 8
1

Better is to add at migration level but if you want to directly add at DB level that is also an option. But update migration as well so that it will have all the columns.

Shakir Baba
  • 343
  • 5
  • 17
  • but the best practice is to create migration file for add or create or update column in database in laravel – mitesh Oct 25 '19 at 10:54