Suppose I have two data rows in my orders table.
`order1 created at 2022-06-18`
`order2 created at 2022-06-19`
Now while I want to search within 2022-06-18 and 2022-06-19 date it shows only order1, but not showing order2 though order2 is also created at 2022-06-19 date.
But If I search within 2022-06-18 and 2022-06-20 then both order1 and order2 row will return as a result.
Here is my laravel query in controller
DB::table('orders')
->whereBetween('orders.created_at', [$from_date, $to_date])
->get();
Is there any problem? Any body help please?
Between always is inlcuding.
In your case the createdat >= '2022-06-18' and <= '2022-06-20'.
Therefore both rows are included
The default created_at and updated_at "timestamp" field created by laravel migration are of type DateTime not just Date.
When you run
->whereBetween('orders.created_at', ["2022-06-18", "2022-06-19"])
It is equivalent to
->whereBetween('orders.created_at', ["2022-06-18 00:00:00", "2022-06-19 00:00:00"])
But your Order2 has created_at with a different hour/minute/second than "00" on the date "2022-06-19".
That's why you get only one result.
To fix it, either add one day ["2022-06-18", "2022-06-20"] or specify the hour/minute/second.
->whereBetween('orders.created_at', [
date('Y-m-d 00:00:00', strtotime($from_date)),
date('Y-m-d 23:59:59', strtotime($to_date)),
])
Related
Anyone can help to put this into a Laravel Eloquent statement ?
where timestamp = unix timestamp
SELECT max(timestamp) ,value FROM `forex` group by FROM_UNIXTIME(timestamp,'%Y%m%d' )
Try this:
DB::table('forex')
->selectRaw('MAX(forex.TIMESTAMP), forex.value')
->groupByRaw('forex.TIMESTAMP, "%Y%m%d"')
->get();
Using Laravel 8 for a restful API.
A table in the database has following fields
report_date , supervisor_id and supervisor_note
I want to retrieve a result that counts the number of reports on any given day date and also counts all of the null values in supervisor_notes field.
I am using the following Laravel code:
$reports=DB::table('reports')
->where('supervisor_id','like', '%'.$supervisor_id.'%')
->select('report_date', DB::raw('count(*) as total'))
->groupBy('report_date')
->get();
I don't know how to combine the code with the second count condition in the same query
You can use conditional aggregation here
$reports=DB::table('reports')
->where('supervisor_id','=', $supervisor_id)
->select([
'report_date',
DB::raw('count(*) as total'),
DB::raw('sum(case when supervisor_note is null or supervisor_note = "" then 1 else 0 end) as notes_total')
])
->groupBy('report_date')
->get();
Also if supervisor_id is integer column and $supervisor_id also holds integer value then use exact integer matching like = instead of using string comparison using like clause
I have a table named shopping_apps, and that table has different entries on different dates. I want to have all records but group all the records on a date basis (not the time part). Suppose I have 20 entries on date1, 12 entries on date2. I want to have only two values extracted from the database: date1 and date2. The controller is below, and Shopping_app is my model name.
<?php
// Use substr() to retrieve date part 10 chars in length.
$shoppingApp = Shopping_app::orderBy('created_at', 'DESC')
->groupBy(substr('created_at', 0, 10))
->get();
ERROR:
SQLSTATE[42000]: Syntax error or access violation: 1055
'laravelpro4.shopping_apps.id' isn't in GROUP BY (SQL: select * from
shopping_apps group by created_at order by created_at desc)
$shoppingApp = Shopping_app::where('created_at', '>=', \Carbon\Carbon::now->subMonth())
->groupBy(DB::raw('Date(created_at)'))
->orderBy('created_at', 'DESC')->get();
try this maybe it will help you
So with the help of #manish I get the correct answer (what I was trying to get)
Error solved by
Adding false
In config\database.php --> "mysql" array
Set 'strict' => false to disable all.
Final code :
$shoppingApp = Shopping_app::where('created_at', '>=', \Carbon\Carbon::now()->subMonth())
->groupBy(DB::raw('Date(created_at)'))
->orderBy('created_at', 'DESC')->get();
This query is for getting other data with the highest value of date with the group by/unique. Here I used unique in place of group by.
My question is how to get min(date) and max(date) with group by/unique.
The group by/unique is for Dataset table's date field.
I have searched for this but not getting exact solution that how to get max and min date with group by/unique in laravel eloquent.
In table structure, there are multiple entries for one code so here I used group by/unique to get one record for the code.
There can be multiple dates as 02-01-2003,01-03-2007,01-01-2019, 01-07-2018... etc. with same/ different code. If I group by with code then get onmy one record per code. So here I want to select the max date [01-01-2019] and min date [02-01-2003].
Thanks in advance.
Controller:
$datasets = Dataset::where('user_id', $user_id)
->orderBy('date','desc')
->get(['code', 'access','user_id','date'])
->unique('code');
Finally I got solution but this can not be the exact solution but as I am beginner and not getting the exact solution I do this functionality as below:
I created two different queries to get min and max values.
$min_datasets = Dataset::where('user_id', $user_id)
->orderBy('date','asc')
->get(['code', 'access','user_id','date'])
->unique('code');
$max_$datasets = Dataset::where('user_id', $user_id)
->orderBy('date','desc')
->get(['code', 'access','user_id','date'])
->unique('code');
Try to select max and min date like this:
$datasets = Dataset::select('code', 'access','user_id', DB::raw('MAX(date) as max_date'), DB::raw('MIN(date) as min_date'))
->where('user_id', $user_id)
->orderBy('date','desc')
->get()
->unique('code');
$data = DB::table('table_name')->where('user_id',$user_id)
->select('code','access','user_id','date')
->whereBetween('date', [02-01-2003, 01-01-2019])
->groupBy('unique_column')
->get()
I want to filter a table by user ID and the created_at field. The only thing is created_at is a timestamp, and I want to query by date only not time.
So something like -
$messages = Message::where(['from' => $fromUser->id, 'created_at' => 'TODAY'S DATE'])->get();
$query->whereDate('created_at', '=', date('Y-m-d'));
or
$query->whereDate('created_at', '=', Carbon::today()->toDateString());
The only way I know (in Laravel) to compare a DATE against created_at is to use whereRaw, so for your example, you'd use something like:
Message::where(...)->whereRaw('DATE(created_at) = ?', [$today])->get();
You can find more information about Eloquent and it's other methods here