chrisgrim's avatar

Adding a column to existing database and using laravel to fill in field

Hi, I am using

php artisan make:migration add_status_to_organizers_table --table=organizers

to add a status column to my organizers model. I already have around 200 organizers in my live app. In my migration file I have

public function up()
    {
        Schema::table('organizers', function (Blueprint $table) {
            $table->char('status', 1)->default('d');
        });
    }
public function down()
    {
        Schema::table('organizers', function (Blueprint $table) {
            $table->dropColumn('status');
        });
    }

Is there any way that I can set it up so that when I run php artisan migrate it will look at each model and if the current date is older than todays date it will set the status to 'p'. Then any new organizers created after will have a default status of 'd'?

0 likes
4 replies
MichalOravec's avatar

What is your whole structure for organizers?

You can after migration just run sql update and change status to those organizers.

chrisgrim's avatar

Hi @michaloravec My structure is

 public function up()
    {
        Schema::create('organizers', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id');
            $table->string('name');
            $table->string('website')->nullable();
            $table->string('email')->nullable();
            $table->string('slug')->unique();
            $table->text('description');
            $table->string('rating')->default(0);
            $table->string('largeImagePath')->nullable();
            $table->string('thumbImagePath')->nullable();
            $table->string('instagramHandle')->nullable();
            $table->string('twitterHandle')->nullable();
            $table->string('facebookHandle')->nullable();
            $table->char('type', 1)->default('o');
            $table->timestamps();
        });
    }

So I would have to run a mysql update, nothing I can do through laravel?

MichalOravec's avatar
Level 75

Of course you can use query builder to update your data in database. It's up to you.

But this "if the current date is older than todays date it will set the status to 'p'" so it means that all organizers in your current database should have set status to p?

Then just update status to p in all rows of organizers.

chrisgrim's avatar

Gotcha, yeah was just wondering if there was anything set in place in case it was a little more complicated than just updating the status to p

Please or to participate in this conversation.