Laravel Fluent Query Builder Update Query - php

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?

You were very close with your fluent attempt, but there were two issues:
The plus sign needs to be inside the quotes, since you want the math done in SQL, not in PHP.
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')]);

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

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.

Related

Yii2 db query builder having into where

Sorry on my English. I add new condition to my query like this:
$query->andWhere(['or',
["<", "o.score", $score_operator],
["<", "d.score", $score_delivery],
["<", "SUM(sd.score)/count(DISTINCT sd.id)",$score_dishes]]);
Buth I got error:
General error: 1111 Invalid use of group function
Then I read that I must use having in 3-d row, but I don't know how to put "having" in "where" condition.
For condition/filter related to aggregation function you should use having and not where
$query->having("SUM(sd.score)/count(DISTINCT sd.id) < " .$score_dishes);
where work on table rows having work on selected result rows.
http://www.yiiframework.com/doc-2.0/yii-db-query.html#having()-detail
starting for 2.0.11 you could use also filteHaving in case you having http://www.yiiframework.com/doc-2.0/yii-db-query.html#filterHaving()-detail

Laravel: like won't work for some reason

Why this query is not working. Executing same thing in sql database returns me this one result that's in image. so doing q select * from kolejka where data like '2017-04-08%' in mysql works. In php artisan tinker it won't work and I cannot figure out why.
When you're using all() first, you get all data and then work with it. like won't work here. So, do this instead:
\App\Kolejka::where('data', 'like', '2017-04-08%')->get();
If you just want to get records by date, just use whereDate():
\App\Kolejka::whereDate('data', Carbon\Carbon::parse('2017-04-08'))->get();

Query in PHP with 'and' or 'or' as variable

I would like to do search for advanced search. The Search feature has and/or for every category. User can choose any combination of and n or. Here i give the screenshot
I store the and/or into variable call $pil, $pil1,$pil2 and $pil3. And will put them in query. it's better than validate one by one every condition of and/or
So this is my query using postgresql in PHP
$query = pg_query("SELECT evaluationdate,onlinename,channel,topik,reviewername,sourceevaluation,evaluation
from AgentPerformance
where onlinename like '%".$VEOn1."%'
".$pil." reviewername like '%".$VERev1."%'
".$pil1." channel like '%".$VEChan1."%'
".$pil2."sourceevaluation like '%".$VESource1."%'
".$pil3."evaluationdate between '".$VEStart1."' and '".$VEEnd1."'");
EDIT :
The problem now, All the variables must not be empty or the query will be error. any way to trick this?
You've missed some spaces near sourceevaluation and evaluationdate
Try with this query :
$query = pg_query("SELECT evaluationdate,onlinename,channel,topik,reviewername,sourceevaluation,evaluation
from AgentPerformance
where onlinename like '%".$VEOn1."%'
".$pil." reviewername like '%".$VERev1."%'
".$pil1." channel like '%".$VEChan1."%'
".$pil2." sourceevaluation like '%".$VESource1."%'
".$pil3." evaluationdate between '".$VEStart1."' and '".$VEEnd1."'");
Simply. Use validation for each $pil whether it is empty or not. it makes me validate 4 times, but it solves the problem. The syntax error has been solved too

Update multiple tables with ZF2 update class

I'm trying to use the zend framework update class: http://framework.zend.com/manual/2.2/en/modules/zend.db.sql.html#zend-db-sql-update
to create something like the statement found here:
http://dev.mysql.com/doc/refman/5.0/en/update.html
UPDATE items,month SET items.price=month.price WHERE items.id=month.id;
I've tried passing an array to ->table but it fails on the string conversion.
// UPDATE `Array`
$update->table(['table1', 'table2'])
I've tried creating an escaped string but it ends up double escaped when converted to sql.
// UPDATE ``table1`,`table2``
$update->table("`table1`,`table2`")
I've tried cheating and used implode to use the glue the tables together
// UPDATE `table1``,``table2`
$update->table(implode("`,`", ['table1','table2']))
Does anyone have a clean answer to this issue?
After further research, I don't think it can be done. The reason I say that is because the query you've proposed may be too resource intensive. After looking for an alternate option, I've come across Update one MySQL table with values from another. However, that I'm aware of, there's no join option on an Update object. So at least at this stage, I don't believe it can be done.
Are you trying to write sql like: "update tableOne, tableTwo set ...." !! I do not think it is possible in sql. As this kind of syntex not supported by mySql so do zend. here is zend table method-
public function table($table)
#param string|TableIdentifier $table
parameter is simply string or TableIdentifier. So you have to write two sql in zend
$update->table('table1');
$update->set(array('foo' => 'bar', 'baz' => 'bax'));
.......
........
$update->table('table2');
$update->set(array('foo1' => 'bar', 'baz1' => 'bax'));
Hope it will help you.

Proper SQL to Laravel ORM

I am newbie on Laravel, I have a proper SQL Statement (which works on PHPMyAdmin and Navicat) and I can get results. What i want to do is, I want to take that statements in Laravel without using DB::raw();.
Any Helps will be appreciated.
Select
rmm.message,
count(rmm.message) as number,
receivedTime as time
FROM
rcs rcs, rmm rmm
WHERE
rmm.smsCid = rcs.smsCid AND rmm.receivedTime LIKE '%2013-04-01%' AND length('rmm.message') > '3'
GROUP BY(rmm.message)
Alternatively you can use the Fluent Query Builder. Here's my attempt, of course it's not checked as I don't have your data structure to hand. Hopefully enough to get you started:
DB::table('rcs') // SELECT FROM rcs
->join('rmm', 'rmm.smsCid', '=', 'rcs.smsCid') // Simple INNER join
->where('rmm.receivedTime', 'LIKE', '%2013-04-01%')
->where(DB::raw('LENGTH(rmm.message)'), '>', 3)
->get(array(DB::raw('COUNT(rmm.message)'), 'rmm.message', 'receivedTime')); // Get only the columns you want
You can use DB::query() to run your query. This calls the query method on the active DB connection.

Categories