Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AlexanderGor's avatar

Сhange default values ​​selectively

Hello! I have a table with the following column: $table->tinyInteger('wear_count')->default(7); I want to change its default value. Found out along the way that tinyInteger cannot be changed. I change with the following command: $table->smallInteger('wear_count')->default(14)->change();

Is it possible to change the default values ​​to something else (14) only for those entries that have not been changed. Or that have not been changed since a certain date. That is, if users have already changed this value for themselves, is it possible to leave them unchanged, and change the rest of the records to 14.

0 likes
14 replies
sr57's avatar

Changing the default value will not change the value of the pre-exsiting records.

1 like
AlexanderGor's avatar

Thanks for the answer. But what if I want to change the previous defaults for entries other than those already changed by the user. Is it possible?

sr57's avatar

Yes, no pb, this default value will be taken in account for the new records.

1 like
newbie360's avatar

you need do a query update all exists records is 7 change to 14

1 like
AlexanderGor's avatar

Thanks, but I asked a little about something else, which is no longer relevant. How can I update all the values ​​of a column in a table?

sr57's avatar

LIke @newbie360 wrote but not filtering of records with 7

in raw sql

update <table> SET <column>=<new_value>;
1 like
newbie360's avatar

also need to add where column = 7

because OP changed some value in that column

1 like
AlexanderGor's avatar

And how to do it in migration with Eloquent? I'm trying this code:

    public function up()
    {
        Schema::table('deals', function (Blueprint $table) {
            $table->smallInteger('wear_count')->default(14)->change();
            $table->update(['wear_count' => 14]);
        });
    }

Why is this code in the Class inherited from Migration not working? The error is displayed: 'Method Illuminate Database Schema Blueprint :: update does not exist.' Which class does the update method belong to?

newbie360's avatar

if you changed the table schema, just put this code in your route, after this actionrun-once-only delete the code in the route

Route::get('/test', function () {
    $count = \DB::table('deals')
              ->where('wear_count', 7)
              ->update(['wear_count' => 14]);

    return $count . ' row updated.';
});

and go to http://your-project/test

then delete the above code

1 like
sr57's avatar

OK @alexandergor we got your problem.

You are confusing 2 things.

  • mrigration is used to create or modify your tables (models) , like default value

  • update data cannot be done by migration, you have to write controller or do it directly in the db (with the tool you want) for 'one shot correction'

1 like
Tray2's avatar

It can be done in the migration but I would say it's a bit of a bad practice.

You could do it in a seeder but that is not quite right either.

However I take it you only want to do it once then I'd use a script directly in the database to handle that rater than complicate it by doing it with php.

If you one the other hand need to run it on multiple servers then I'd probably put it in a seeder.

1 like
sr57's avatar

Good evening @tray2

It can be done in the migration

Everything can be done everywhere ... I open a new post

1 like
newbie360's avatar

yep, since OP need to run-once-only, this can be done by phpMyAdmin, php, php artisan tinker

1 like
AlexanderGor's avatar

Thank you all for your help. I work more with the frontend, but for the project I had to deal with Laravel a little, so I ask such questions ... In my case, it is better to use a SQL command to change the data in the table and one new Migration to set a new default value for the column. Thank you again for your help, a good team has gathered on this forum :)

Please or to participate in this conversation.