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?
To increment or decrement a column value using Laravel's query builder, you can use the method increment()
and decrement()
.
Eg:
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.
You can try this like way
public function up(){
DB::table('mytable')
->update([
'amount' => $amount++,
]);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.