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?

4
  • you can use migration as shown in this answer, or add it directly using the dbms. may we know what dbms you are using? Commented Oct 25, 2019 at 7:04
  • thanks for the suggestion. we are using mysql Commented Oct 25, 2019 at 7:37
  • if you are using raw mysql without laravel migrations, you can use ALTER TABLE query, something like ALTER TABLE ADD COLUMN "my-new-column" varchar(MAX) or something... Commented Oct 25, 2019 at 7:51
  • Best solution is to use a framework the way they intent you to use it. I would strongly suggest using laravel's migrations. Commented Oct 25, 2019 at 12:40

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

but the best practice is to create migration file for add or create or update column in database in laravel

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.