Ad

How To Use Increment Function In Laravel

i am using DB to store values in database.

i have "course fees" column i what to "increment" the "course_fees" value in column.

for example

 DB::table('student')->where('registration_id','=', $request->registration_id)->increment(['course_fees' =>$request->course_fees]);

this code increment the inserted value

how can i modified below code for increment "course_fees" value like above

      DB::table('student')->where('registration_id','=', $request->registration_id)->update(['payment_date' => $request->payment_date,'balance_fees' => $request->balance_fees,'course_fees' =>$request->course_fees]);
Ad

Answer

increment()/decrement() can take 3 parameters: $column, $amount, $extra.

$column is the field that you want to increment
$amount is by how much you want to increment the field by
$extra is an array of attributes that you also want to update in the query.

If you don't pass an amount the default for $amount is 1.


To achieve what you're after you could do:

DB::table('student')
    ->where('registration_id', $request->registration_id)
    ->increment('course_fees', $request->course_fees, [
        'payment_date' => $request->payment_date,
        'balance_fees' => $request->balance_fees,
    ]);
Ad
source: stackoverflow.com
Ad