2

I want to add two columns while using update, like this:

 Update purchase_stock inner join stock on purchase_stock.fkstockid=stock.stockid SET currentavailable=currentavailable+subquantity where fkorderid='1';

Here is the current Fluent code:

 DB::table('purchase_stock')->join('stock','stock.stockid','=','purchase_stock.fkstockid')->where('fkorderid',$orderId)->update(array('currentavailable'=>'currentavailable'+'subquantity'));**

But it throws error as below:

"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"syntax error, unexpected '=>'"

Does any one have solution?

3 Answers 3

1

You were very close with your fluent attempt, but there were two issues:

  1. The plus sign needs to be inside the quotes, since you want the math done in SQL, not in PHP.
  2. Need a DB::raw() around the value so Laravel doesn't think you're actually trying to set it to the string "currentavailable + subquantity"

So the final product looks like this:

DB::table('purchase_stock')
    ->join('stock', 'stock.stockid', '=', 'purchase_stock.fkstockid')
    ->where('fkorderid', $orderId)
    ->update(['currentavailable' => DB::raw('currentavailable + subquantity')]);
Sign up to request clarification or add additional context in comments.

Comments

0

Мaybe you need to set these two fields from which tables. Exampl. DB::table('purchase_stock')->join('stock','stock.stockid','=','purchase_stock.fkstockid')->where('fkorderid',$orderId)->update(array('stock.currentavailable'=>'stock.currentavailable'+'stock.subquantity'));

Comments

0

Ohk!
I already tried this one but it is not working
As of now I am using DB::statement('') and it's working
So I have written whole update query within statement and it's working as somewhere I have read that it will not be helpful to return result set but will work with insert or update.

1 Comment

It's not a best practice

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.