E
E
Evgeniy Golovin2021-09-22 00:43:31
Database migration
Evgeniy Golovin, 2021-09-22 00:43:31

How to update one column when migrating in laravel?

I study Lara and database migrations in it. Faced a problem with updating columns in the database (ALTER TABLE)

Let's say I have a blog migration

in function up a simple table

Schema::create('blog', function (Blueprint $table) {
            $table->id();
            $table->char('title', 150);
            $table->timestamps();
});


Then I do
php artisan migrate

After I filled the table with data and suddenly I was impatient to add a column, say description

Question: how to do this?

Tried by "table" in function up(){}, after create
Schema::table('blog', function (Blueprint $table) {
            $table->text('description');
});


But it doesn't come out. What is the correct way to do ALTER TABLE without losing all the data?
Since using php artisan migrate:refresh it will wipe everything (

PS. I read that you can create another migration and only edit it (via Schema::table()) of the blog table

. I just thought that the correct structure of migrations is one table = one migration.If not, then how not to get confused then in all migrations.Do not call them blog_add_desc and so on

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JhaoDa, 2021-09-22
@JonGol

I read that you can create another migration and only edit the blog table in it
Not possible, but necessary.
I just thought that the correct structure of migrations is one table = one migration
No.
Do not invite them blog_add_desc and so on
Just call it like that.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question