skip to Main Content

Hello i want to update a single value on another table using laravel. This is the code i have done until now but doesnt seem to work:

$amount = Product::findorFail($request->products[$i]);
$total_value = $request->amount[$i] + $amount->amount;
$amount->update(['amount', $total_value]);
dd($total_value);

with dd i see that the result is correct but the update function is not, the query im trying to make is

update table set amount=x where id=y

2

Answers


  1. you could change your code like below

    $amount = Product::findorFail($request->products[$i]);
    $total_value = $request->amount[$i] + $amount->amount;
    $amount->amount=$total_value;
    $amount->update();
    

    or as mentioned in comments you could use eloquent increment function

    Login or Signup to reply.
  2. You have multiple choices. The shortes are:

    $amount->update(['amount'=> $amount->amount + request->amount[$i]]);
    

    or

    Product::findorFail($request->products[$i])->increment('amount', $request->amount[$i]);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search