Udaya
Udaya

Reputation: 11

How to add column to existing table in laravel?

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?

Upvotes: 0

Views: 3471

Answers (2)

Nick
Nick

Reputation: 546

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

Upvotes: 3

Shakir Baba
Shakir Baba

Reputation: 373

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.

Upvotes: 1

Related Questions