Pratik Shah
Pratik Shah

Reputation: 149

Laravel Fluent Query Builder Update Query

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?

Upvotes: 2

Views: 1792

Answers (3)

mopo922
mopo922

Reputation: 6381

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')]);

Upvotes: 1

Pratik Shah
Pratik Shah

Reputation: 149

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.

Upvotes: 0

Fortran
Fortran

Reputation: 593

М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'));

Upvotes: 0

Related Questions