简体   繁体   中英

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM