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.
Related
Is there any way to put a manual function inside a query in Laravel.
I've timestamp saved in string in DB. I want to convert timestamp from one timezone to another. All the timestamp is inserted in one time zone, and depending upon my user I fetch the timestamp and convert it into their timezone.
what I want to achieve is something like this..
$query = BlogCategory::select('merchant_id', userTime(added_at))
->where('site_id', $site_id)
->get();
userTime() function takes two parameter, the timestamp and the timezone and converts the timsestamp to time of the user.
I want to use userTime() function before fetching the data. I dont want to fetch the data first and then do foreach and so on.
I know I might be absolutely absurd but is there anything of this sort in Laravel?
Well you can achieved that using collection map
$query = BlogCategory::select('merchant_id', 'added_at')
->where('site_id', $site_id)
->get();
$dateAdded = $query->map(function ($data) {
// try this if error $data['merchant_id']
return array(
'merchant_id' => $data->merchant_id,
'added_at' => $this->userTime($data->added_at)
);
})
dd($dateAdded);
Read Collection documentation here: https://laravel.com/docs/5.8/collections
You should use the selectRaw statement and let your DB do this logic for you if you don't want to loop over the result set.
For example if your underlying database is MySQL you can use the CONVERT_TIMEZONE function and do something like this.
BlogCategory::selectRaw('merchant_id, CONVERT_TZ(added_at, "GMT", "MET") as added_at')
->where('site_id', $site_id)
->get();
I currently have a table called operators
The columns are:
id, user_id, item_clicked, created_at, updated_at
I can confirm on one of these it has today's update_at
2019-08-05 showing as today.
This is a relational table.
operators belongs to users.
I'm attempting to get operators of users whereDate is today.
My code is:
$ordersClicked = User::with('operators')
->whereDate('updated_at', \Carbon\Carbon::today())
->get();
This returns an empty array.
Testing this works so I know the data I need is there.
$ordersClicked = User::with('operators')
->get();
$operator = Operator::whereDate('updated_at', \Carbon\Carbon::today())->get();
Any idea what I'm doing wrong?
According to the docs https://carbon.nesbot.com/docs/ Carbon::today() returns Y-m-d H:i:s, if used like you are doing in a where clause and the database stores the value in Y-m-d format.
Why don't you try this:
$ordersClicked = User::with('operators')
->whereDate('updated_at', \Carbon\Carbon::today()->format('Y-m-d'))
->get();
if that doesn't work; then it is possible the eloquent is applying the where clause on updated_at on the User table, instead of Operator. Did you mean to do something like this:
$ordersClicked = User::with(['operators' => function($query) {
$query->where('updated_at', \Carbon\Carbon::today()->format('Y-m-d'))
}])->get();
i.e. filter on the eager loaded collection? if this is what you intended, what will happen is it will return all users and only some operators that has the updated_at matching the criteria will have values, where others will be null.
Wouldn't it be better to reverse the query? i.e. make sure Operator has a belongsTo relationship on User model and query the data like this:
$operator = Operator::whereDate('updated_at', \Carbon\Carbon::today())
->with('user')
->get();
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
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();
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