skip to Main Content

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?

2

Answers


  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.

    Login or Signup to reply.
  2. 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search