Laravel: like won't work for some reason - php

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();

Related

Select month from a date field in laravel

I'm trying to perform a query in Laravel 7 that gets the month part of a date field. I tried the following queries
$test = MyModal::selectRaw('month("date_col") as temp')->get();
$test = MyModal::select(DB::Raw('month("date_col") as temp'))->get();
$test = DB::table('my_table')->select('month("date_col") as temp')->get();
All variation of the same query. If I dump the query log, the query is correct for all 3
select month("date_col") as temp from "my_table"
If I run this on my DB I get the result as well. But I keep getting this error in laravel:
Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 no such function: month (SQL: select month("date_col") as temp from "my_table")
Am I doing something wrong? And is there a way to do this query in laravel?
Edit: Sorry forgot to mention this is with Laravel's unit testing
I think the problem in the way you use Month function
you don't set quotations for the column name, it should be:
$test = MyModal::selectRaw('month(date_col) as temp')->get();
it should work
You can do something like this:
MyModal::select(DB::raw("MONTH(date_col) month"))->get();
Thanks to OMR's comment this solution finally worked
$test = MyModel::selectRaw('strftime("%m", "date_col") as temp')->get();
Update: It looks like what I had in the beginning worked fine and the unit test was the only part throwing this error. Since I was working with mysql while the test environment was using sqlite which apparently doesnt like the "month()" function which OMR did point out in his comment. So this solution works in the test but not in live. Couldn't figure out how to change the test environment to use in memory mysql instead of sqlite. If anyone knows how to please add an answer here.

Laravel 5.1 Eloquent whereBetween not working as expected

I have to get rows within a range of passed symbol no.
Image of Table from where query is to be done
Query:
Mark::select('users_id', 'symbol_no', 'mark_obtained')
->where('subject_trade_id', 2)
->whereBetween('symbol_no', [100, 1000])
->orderBy('symbol_no')
->get();
This query returns no data, but I was expecting total 9 rows form the query.
If I dump the query, I find the query as expected.
Query Log Image
If I run the generated query to mysql then it woks fine.
Again, if I change the symbol no. range to something like this:
Mark::select('users_id', 'symbol_no', 'mark_obtained')
->where('subject_trade_id', 2)
->whereBetween('symbol_no', [10, 1011])
->orderBy('symbol_no')
->get();
It returns 2 rows this time, and this output is also wrong.
If I try changing symbol no. range and query like this:
Mark::select('users_id', 'symbol_no', 'mark_obtained')
->where('subject_trade_id', 2)
->whereBetween('symbol_no', [101, 200])
->orderBy('symbol_no')
->get();
Now it works fine as expected.
Found the problem
By mistake the symbol_no column was defined as varchar() which had to be int() so, whereBetween() was being unable to return expected data.
I faced same problem.. Could you please check "symbol_no" field type in database table, It should be number/integer.

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?
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.

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.

Yii2: Get raw sql of $query->count('*') in Yii2

I want to get the raw query for $query->count("*").
I have tried
$s=$query->count("*");
$s=$s->createCommand()->sql;
It does not work.
Usually I do $s=$query->createCommand()->sql; to get raw sql which works fine. How to get it for count('*'). Please help.
You can't use createCommand() on $s, because count() method returns integer|string, it's not an object. To get count sql, you can use:
$query->select('count(*)')->createCommand()->sql

Categories