user3174311
user3174311

Reputation: 1993

How to update a value using Laravel migrations?

I would like to add 1 to all the values in a table. I am doing this:

public function up()
{
    DB::table('mytable')
        ->update([
            'amount' => amount + 1,
        ]);
}

the above code does not work. How should I refer to a value (amount) using migrations?

Upvotes: 2

Views: 112

Answers (2)

yivi
yivi

Reputation: 47349

To increment or decrement a column value using Laravel's query builder, you can use the method increment() and decrement().

E.g.:

DB::table('mytable')->increment('amount');

This is documented here.

Not sure if doing it on a migration is the wisest thing. I would reserve migrations to schema changes, not to data changes.

Upvotes: 2

A.A Noman
A.A Noman

Reputation: 5270

You can try this like way

public function up(){

    DB::table('mytable')
       ->update([
           'amount' => $amount++,
       ]);
}

Upvotes: 0

Related Questions