Whats wrong with my Laravel eloquent query - laravel

What's wrong with my Laravel query, it is giving one less result than original result.
Test::where('has_ftd', 1)
->where('ftd_date', '>=', '2018-10-17')
->where('ftd_date', '<=', '2018-10-17')
->paginate(10);
Update: I Just noticed that less than equal to '<=' is giving less than '<' result

When querying datetime or timestamp values, it's generally important to include the "time" portion to ensure you're querying the correct values:
Test::where('has_ftd', 1)
->where('ftd_date', '>=', '2018-10-17 00:00:00')
->where('ftd_date', '<=', '2018-10-17 23:59:59')
->paginate(10);
Providing the values 2018-10-17 00:00:00 and 2018-10-17 23:59:59 will ensure the query handles values for a full, inclusive date.

For ranges you can use Laravel whereBetween() method. you can try this:-
Test::where('has_ftd', 1)
->whereBetween('ftd_date', [$date_from, $date_to])
->paginate(10);
I think this will work.
Ref: https://laravel.com/docs/5.7/queries#where-clauses

Related

Eloquent whereDate and where clause return empty

There's a few answers to this but none seem to help.
Currently I have records in my database that are before the date specified.
$today = Carbon::now();
$dtB = $today->subWeekdays(8);
a dd of $dtB returns: date: 2019-09-30 11:56:44.0 America/Los_Angeles (-07:00)
My attempt to get records from any date BEFORE where completed is NOT true is:
$orders = JobStatus::orderBy('shopify_date','desc')
->where('completed', '!=', 'true')
->whereDate('shopify_date', '<', $dtB)
->simplePaginate(25);
A dd of $orders returns empty.
If I remove the where clause I get the data.
Another post answer suggested removing whereDate and using where.
$orders = JobStatus::orderBy('shopify_date','desc')
->where('completed', '!=', 'true')
->where('shopify_date', '<', $dtB)
->simplePaginate(25);
This also returned empty.
What am I missing here?
EDIT: Date in db:
2019-07-18T15:59:09-07:00
If I do whereNull('completed) along with whereDate('shopify_date', '<', $dtB) it works. However I need where completed not equal to 'true', the system will allow true, false or null.
I think the problem is that $dtB is not in the same format as 'shopify_date', which I suppose is being stored on the database as Y-m-d H:i:s, correct?
With that in mind you can try using date() function instead of Carbon in this case. Like so:
$dtB = date('c', strtotime('-8 days'));
It will output something like 2019-09-30 16:17:15, which is in the same format as 'shopify_date', and thus the eloquent query will probably work.
Let me know if this solves the problem.

How to make grouped request by only day part of created_at?

In my my laravel 5.7.3 application , I need to get grouped date like
$voteItemUsersResults = VoteItemUsersResult::orderBy('created_at', 'desc')
->groupBy('created_at')
->having('created_at', '>', '2018-09-01')
->get();
but I need to get only day part of created_at, without time.
Which is the valid way to make it ?
Grouped by Only Day of Datetime .
$voteItemUsersResults = VoteItemUsersResult::orderBy('created_at', 'desc')
->groupBy(DB::raw('DATE_FORMAT(created_at, "%d")'))
->having('created_at', '>', '2018-09-01')
->get();

Eloquent where between dates in Laravel

I`m having trouble with an Eloquent query.
The structure of my Model is:
Coupon (for get discounts for a booking):
period_start (coupon is valid for some specific periods)
period_end (this end date, for example: the coupon is valid when you make a booking between 2018-02-01 and 2018-03-03)
booking_start (the period of when the booking is done)
booking_end (you can book for example from 2018-01-15 till 2018-01-30 only for the coupon to be valid)
Code:
$coupon = Coupon::where('period_start', '<=', $request->get('from_date'))
->where('period_end', '<=', $request->get('to_date'))
->where('booking_start', '<=', now()->format('Y-m-d'))
->where('booking_end', '>=', now()->format('Y-m-d'))
->first();
In the order field people pick two dates from a datepicker, thats where the request()'s are for.
This should work, but I don't get any results...
What about using dateBetween?
$start = Carbon::parse($request->start)->startOfDay(); //2016-09-29 00:00:00.000000
$end = Carbon::parse($request->end)->endOfDay(); //2016-09-29 23:59:59.000000
$clicks->dateBetween($start, $end);
https://laracasts.com/discuss/channels/eloquent/daterange-with-wherebetween
You should re format the date that get from the datepicker before query part, like this.
$date=date('Y-m-d', strtotime(str_replace('-', '/', $request->to_date)));

Select Query - Upto Previous Date Records is Not Working in Laravel

Following Laravel Query I Write for to get Upto Previous Date Records that's not getting any Records. If I Remove Date query its get Many Records.
$data['frmdate_submit'] format is 2017-05-24.
How to Fix this Problem
$getpreviousbalance = Companyledger::where('transaction_date', '>', $data['frmdate_submit'])
->WhereIn('frm_ledger', $ledgerlist)
->where('company_id', $companyids)
->get();
Use whereData instead of where when you are checking with dates.
$getpreviousbalance=Companyledger::whereDate('transaction_date','>',$data['frmdate_submit'])->WhereIn('frm_ledger',$ledgerlist)->where('company_id',$companyids)->get();
hope this will works.
$getpreviousbalance = Companyledger::where('company_id', $companyids)
->whereIn('frm_ledger', $ledgerlist)
->where('transaction_date', '>', \Carbon\Carbon::parse($data['frmdate_submit']))
->get();

Laravel/Eloquent and comparing dates

I want to return all of the rows in my database table that are a day or less old. I'm using Laravel 4. This is what I tried:
$date = date('Y-m-d H:i:s');
return MainContact::where(DATEDIFF('timestamp', $date), '<=', 1)->get();
This doesn't work. I read the documentation and it doesn't seem like you can pass Laravel MySQL functions. timestamp is a datetime field. How can I compare these dates in Laravel 4?
The answer that user1977808 gave you is not good because MySQL can't use an index on the timestamp column, since it has to compute an output of the DATE_SUB function for every row. Avoid such queries, they have to process the entire table every time!
How about something like this:
return MainContact::where('timestamp', '>=', time() - (24*60*60))->get();
I put the >= in there because you said "a day or less old", so they must have timestamp that is later than yesterday.
Alternatively,
You can use Carbon API that bundle with Laravel.
ModelName::where( 'timestamp', '>=', Carbon::now() )->get();
Reference: http://laravel.com/docs/5.1/eloquent-mutators
You could also use whereDate(), whereDay(), whereMonth() and whereYear(). In this case, whereDate() could be used as such, with Carbon's easy date functions:
return MainContact::whereDate('dateField', '<', Carbon::now()->subDay())->get();
return MainContact::where('timestamp', '>=', time() - (24*60*60))->get();
You can also do a raw query by using:
$results = DB::query( 'query' );
You only don't the the model object back in the results var

Resources