Reputation: 1993
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
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
Reputation: 5270
You can try this like way
public function up(){
DB::table('mytable')
->update([
'amount' => $amount++,
]);
}
Upvotes: 0