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 ? "
Related
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();
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();
I need to select entries based on dates happening in the future and the
entries contain the date format:
12/30/17
I'm trying to format the date and compare to Carbon::now() timestamp, with no luck.
$now = \Carbon\Carbon::now();
$bookings = DB::table('booking')
->select('booking.*')
->where('booking.uid', '=', Auth::id())
->where(DB::raw("(DATE_FORMAT(booking.date,'%Y-%m-%d 00:00:00'))"), ">=", $now)
->get();
You'll need to use STR_TO_DATE to convert the string.
$bookings = DB::table('booking')
->select('booking.*')
->where('booking.uid', '=', Auth::id())
->where(DB::raw("(STR_TO_DATE(booking.date,'%m/%d/%y'))"), ">=", $now)
->get();
STR_TO_DATE will convert 12/30/17 to 2017-12-30
I don't think you really need to check date format. Also, you have some redundand stuff in the query. Just do this:
Booking::where('uid', auth()->id())->where('date', '>=', now())->get();
And if the date format is really different in some of the rows in the same column, you really need to fix this and instead of making some dirty fix for that.
$bookings = DB::table('booking')
->select('booking.*')
->where('booking.uid', '=', Auth::id())
->where(DB::raw("(DATE_FORMAT(booking.date,'%Y-%m-%d'))"), ">=", $now)
->get();
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();
I am trying to query the 'created_at' field by the date for today:
$today = DATE('Y-m-d');
$logs = DB::table('bglogs')->where('DATE('created_at')','=',$today)->where('user_id','=',Auth::user()->id)->get();
It tells me that DATE('created_at') is an unknown column? Any suggestions, relatively new to Eloquent so I am sure I've missed something obvious.
Thanks in advance!
EDIT: 6/10/2014
Wanted to note that I had to tweak it a bit as it was bringing back all records rather than a specific date. Not sure why. I finally got this working correctly. Thanks again to all who answered and I hope this update will help others in the future:
$logs = DB::select(DB::raw("SELECT * FROM bglogs WHERE DATE(created_at) = :today AND user_id = :user"), array('today'=>DATE('Y-m-d'), 'user'=>Auth::user()->id));
If you want to use mysql functions you must use whereRaw and wite it in a single string.
In the other where, you can skip the second parameter if it will be equals (=).
$today = DATE('Y-m-d');
$logs = DB::table('bglogs')
->select(DB::raw('*'))
->whereRaw("DATE('created_at') = " . $today)
->where('user_id', Auth::user()->id)
->get();
Hope its help you.
I recommend you dont declare alias if you will use the var just one time:
$logs = DB::table('bglogs')
->select(DB::raw('*'))
->whereRaw("DATE('created_at') = " . DATE('Y-m-d'))
->where('user_id', Auth::user()->id)
->get();
You may try this (Carbon is available with Laravel):
$today = Carbon\Carbon::toDay()->toDateTimeString();
$logs = DB::table('bglogs')->where('created_at', $today)
->where('user_id', Auth::user()->id)
->get();
there is no need to use raw method when you can just define search criteria.
try this:
$today = DATE('Y-m-d');
$logs = DB::table('bglogs')
->where('created_at', '=>', $today.' 00:00:00')
->where('created_at', '<=', $today.' 23:59:59')
->where('user_id', '=', Auth::user()->id)
->get();
if problem still exists so check your table for existence of this created_at field.
$today=date("Y-m-d");
$coustomers=Coustomer::where('created_at','like',"$today%")->get();
You can use Carbon class provided by laravel:
$today = Carbon::today();
$logs = DB::table('bglogs')
->where('created_at','>=',$today)
->where('user_id','=',Auth::user()->id)
->get();
Or
You can use php date functions to do this manually:
$today = new \DateTime(date('F jS Y h:i:s A', strtotime('today')));
$logs = DB::table('bglogs')
->where('created_at','>=',$today)
->where('user_id','=',Auth::user()->id)
->get();