Laravel `->whereDate` doesnt include today - php

Laravel ->whereDate doesnt include today.
I have 2 datetime col on my DB (start and end). I use this to query
$result = BlaModel::where('status', '=', "bla")
->whereDate('start', '<=', $date)
->whereDate('end', '>', $date)
->get();
The $date variable is a string 2020-03-25 11:23:09 and I have an entry on my db with
start - 2020-03-18 10:44:00
end - 2020-03-25 20:59:00
Question: Why does this query does not fetch that entry. Any advice will be helpful. Thanks.

Because whereDate will change to raw sql with mysql inbuilt-method DATE():
select *
from `blas`
where date(`start`) <= '2020-03-25 11:23:09'
and date(`end`) > '2020-03-25 11:23:09'
So date(2020-03-25 20:59:00) => 2020-03-25 which is less than '2020-03-25 11:23:09'.
You can change your code to
$result = BlaModel::where('status', '=', "bla")
->where('start', '<=', $date)
->where('end', '>', $date)
->get();

Try this
If you want use WhereDate write your query like.
$result = BlaModel::where('status', '=', "bla")
->whereDate('start', '<=', \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', '2020-03-18 10:44:00')->format('Y-m-d'))
->whereDate('end', '>',\Carbon\Carbon::createFromFormat('Y-m-d H:i:s', '2020-03-25 20:59:00')->format('Y-m-d'))
->get();
Or use Simple Where like
$result = BlaModel::where('status', '=', "bla")
->where('start', '<=', $date)
->where('end', '>', $date)
->get();

Related

Laravel 7 DB Query only where date range not in date range

I have something like this:
$from = min($start_date, $end_date);
$till = max($start_date, $end_date);
DB::table('booked')
->where('start', '<=', $from)
->where('end', '>=', $till)
->get();
This is a nice solution form stackoverflow.
It seems works, but I need the opposite.
In my DB I have this data: start: 2020-09-27 10:00:00 end: 2020-09-27 12:00:00
I have to query that rows where for example:
$start_date(2020-09-27 9:00:00) and $end_date(2020-09-27 11:00:00) where both not in start: and end: Hopefully my question is clear enough.
If I am getting you right this is what you need
$from = min($start_date, $end_date);
$till = max($start_date, $end_date);
DB::table('booked')
->where('start', '>=', $till)
->orWhere('end', '<=', $from)
->get();

How to type within today From 00:00:00 to 23:59:59 in Laravel

How to add "within today" (i.e. From 00:00:00 to 23:59:59) in Laravel ?
$grpcnt = DB::table('groups')
->where('owner_id', $u_id)
->whereBetween('created_at', xxx,xxx)
->count();
If you want to get count of today group then you may use like this.
DB::table('groups')->where('owner_id', $u_id)
->whereDate('created_at', Carbon::today())
->count();
Or as per your requirement
$start = Carbon::now()->startOfDay(); //2019-07-27 00:00:00.000000
$end = Carbon::now()->endOfDay(); //2019-07-27 23:59:59.000000
DB::table('groups')->where('owner_id', $u_id)
->whereBetween('created_at', [$start, $end])->count();
You can use carbon for this, with start of the day and end of the day
$grpcnt = DB::table('groups')
->where('owner_id', $u_id)
->whereBetween('created_at', [ Carbon::now()->startOfDay(),Carbon::now()->endOfDay()])
->count();

How to use WhereBetween date with %LIKE% in sql query

In my database creadted_at data 2017-11-07 18:58:16,2017-11-07 19:58:16. I try to use WhereBetween for searching data in date 2017-11-07
Am not sure How can I put Like % % into my query
'like', '%'.2017-11-07.'%'
->WhereBetween('created_at', ['2017-11-07', '2017-12-07'])
Here is my full controller
$b = DB::table("v_dealer_sell_time")
->select([
'product_name',
DB::raw('COALESCE(SUM(total_price), 0) AS total_price'),
DB::raw('COALESCE(SUM(total_product), 0) AS total_product')
])
->WhereBetween('created_at', ['2017-11-07', '2017-11-07'])
->groupBy('product_name')
->get();
If you want the created ones in the same given date you can use whereDate like this But since 5.3 (Documentation):
$b = DB::table("v_dealer_sell_time")
->select([
'product_name',
DB::raw('COALESCE(SUM(total_price), 0) AS total_price'),
DB::raw('COALESCE(SUM(total_product), 0) AS total_product')
])
->WhereDate('created_at', '=', $date)
->groupBy('product_name')
->get();
If you want it from between two dates use whereBetween like this :
$b = DB::table("v_dealer_sell_time")
->select([
'product_name',
DB::raw('COALESCE(SUM(total_price), 0) AS total_price'),
DB::raw('COALESCE(SUM(total_product), 0) AS total_product')
])
->WhereBetween('created_at', [Carbon::parse('2017-11-07')->startOfDay(), Carbon::parse('2017-12-07')->endOfDay()])
->groupBy('product_name')
->get();
PS : Do not forget to add use Carbon\Carbon; at the top of your Controller.
Use whereDate():
$date = Carbon::parse($date)->toDateString();
....
->whereDate('created_at', $date)
Or whereBetween():
$date = Carbon::parse($date);
$from = $date->copy()->startOfDay();
$to = $date->copy()->endOfDay();
....
->whereBetween('created_at', [$from, $to])
If this Date search related to a Search Function, then you have to do something like this
Code
Change this to
->WhereBetween('created_at', ['2017-11-07', '2017-11-07'])
this
->WhereBetween('created_at', ['2017-11-07 00:00:00', '2017-11-07 23:59:59'])
WHY ??
2017-11-07 00:00:00 - Start of the day
2017-11-07 23:59:59 - end of the day
if you use tosql() and check your query it has something like this
.`created_at` between ? and ? "

Laravel 5.4 query builder Missing argument 2 when using orWhere clause

Can someone tell me why im getting this error and how to fix it please.
$lastDayPreviousMonth = date("Y-m-d", strtotime("last day of previous month"));
$firstDayPreviousMonth = date("Y-m-d", strtotime("first day of previous month"));
$query = DB::table('employees')
->where('Emp_ClientId', '=', $clientId)
->where('Emp_StatusId', 1)
->orWhere(function ($query, $firstDayPreviousMonth, $lastDayPreviousMonth){
$query->where('Emp_DateSuspTerm', '>=', $firstDayPreviousMonth)
->where('Emp_DateSuspTerm', '<=', $lastDayPreviousMonth)
->where('Emp_ClientId', '=', $clientId);
})
->count();
Im getting the following error when i run this
Missing argument 2 for App\Http\Models\Employees::App\Http\Models{closure}()
I think it has to do with the firstdaypreviousmonth and lastdaypreviousmonth parameters im passing into the orWhere clause - if i take it out i get undefined variable.
You ca n use closure using use keyword
$query = DB::table('employees')
->where('Emp_ClientId', '=', $clientId)
->where('Emp_StatusId', 1)
->orWhere(function ($query) use($firstDayPreviousMonth, $lastDayPreviousMonth,$clientId){
$query->where('Emp_DateSuspTerm', '>=', $firstDayPreviousMonth)
->where('Emp_DateSuspTerm', '<=', $lastDayPreviousMonth)
->where('Emp_ClientId', '=', $clientId);
})
->count();

Laravel - where less/greater than date syntax

This is not showing the correct count. What is the correct syntax ?
$this->data['Tasks'] = \DB::table('tb_tasks')->where('Status', 'like', 'Open%')->whereDate('DeadLine', '>', 'CURDATE()')->count();
Use a Carbon instance:
$this->data['Tasks'] = \DB::table('tb_tasks')->where('Status', 'like', 'Open%')->whereDate('DeadLine', '>', Carbon::now())->count();
You can also use the now() helper
$this->data['Tasks'] = \DB::table('tb_tasks')->where('Status', 'like', 'Open%')->whereDate('DeadLine', '>', now())->count();
Use DB::raw:
->where('datefield', '>', \DB::raw('NOW()'))
We can also try this one. It works for me.
$date = "2020-04-10";
/*
Assumimng DB `login_date` datetime format is "Y-m-d H:i:s"
*/
$from_date = $date.' 00:00:01';
->where('login_date', '>=', $from_date);
By adding Where Clause in the query, we can find the result having
rows after the particular date.
Option-2:
$date = "2020-03-25"; // Format: date('Y-m-d);
$orders = DB::table('orders')
->select('*')
->whereDate('order_datetime', '<=', $date)
->get();
// Here, Table Field "order_datetime", type is "datetime"
// Assuming DB `order_datetime` stores value format like: "Y-m-d H:i:s"
you can make use of whereDate like below:
$query->whereDate('DeadLine', '>', Carbon::now())->count();

Categories