Laravel eloquent query convert created_at datetime to date - laravel

i am querying the database the get my tracker results:
$trackers = TrackerResult::all()
->where('tracker_id', $id)
->where('created_at', '>=', Carbon::now()->subDays(30));
return $trackers;
But this returns me "created_at" field as datetime as its by default. Is there are way to get just the date instead? ('Y-m-d') format.

If you want to compare date only try ->whereDate():
TrackerResult::all()
->where('tracker_id', $id)
->whereDate('created_at', '>=', Carbon::now()->subDays(30));
You could also manually format it using ->format():
TrackerResult::all()
->where('tracker_id', $id)
->where('created_at', '>=', Carbon::now()->subDays(30)->format('Y-m-d'));

you should use DATE function:
$trackers = TrackerResult::all()
->where('tracker_id', $id)
->whereDate('created_at', '>=', Carbon::now()->subDays(30))
->select('column1',column2',DB::raw('Date(created_at) as formattedCreateAt'))
->get();
return $trackers;

Related

Compare date time in Laravel eloquent

I have start and end date time column in database
start_date end_date contact
2021-01-25 17:30:00 2021-01-25 20:30:00 xxxxxxxxx
2021-01-27 17:30:00 2021-01-28 19:30:00 xxxxxxxxx
Now i'm trying to get records which exists between these two date time, it should match mobile number also.I'm using below query but it is not filtering record
$date = date('Y-m-d H:i:s', strtotime($request->date));
$contact_number=$request->contact_number;
$event=Event::where(function ($query)use($date,$contact_number){
$query->where('start_date', '>=', $date)->where('end_date', '<=', $date);})->where('contact',$contact_number)->paginate(5);
You have to invert the >= and <= signs
$date = date('Y-m-d H:i:s', strtotime($request->date));
$contact_number = $request->contact_number;
$event = Event::where(function ($query) use ($date, $contact_number){
$query->where('start_date', '<=', $date)
->where('end_date', '>=', $date);
})
->where('contact',$contact_number)
->paginate(5);
Laravel 4+ offers you these methods: whereDay(), whereMonth(), whereYear() and whereDate() .
Here are some ways to make the comparison :
->where('date', '<=', '2014-07-10 23:59:59')
->where('date', '<', '2014-07-11')
// '2014-07-11'
$dayAfter = (new DateTime('2014-07-10'))->modify('+1 day')->format('Y-m-d');
->where('date', '<', $dayAfter)
Try this:
$date = Carbon\Carbon::parse($request->date)->format('Y-m-d H:i:s');
$contact_number=$request->contact_number;
$event=Event::where(function ($query)use($date,$contact_number){
$query->where('start_date', '<=', $date)->where('end_date', '>=', $date);})->where('contact',$contact_number)->paginate(5);

Laravel Eloquent whereTime() shows null

I have this query to get the events after the the current time and it shows as null. I think the whereTime condition is not working or I have a typo.
$query = Model::where('id', $request->id)
->where('status_id', 1)
->whereTime('event_date', '>=', Carbon::now())
->first();
When I don't have the whereTime condition and check the results, it shows this result
$query = Model::where('id', $request->id)
->where('status_id', 1)
// ->whereTime('event_date', '>=', Carbon::now())
->first();
dd(Carbon::now()->format('Y-m-d H:i:s'), $query->event_date);
Result
"2020-04-15 20:07:43"
"2020-05-23 18:00:00"
If your date like this "2021-03-21 13:33:52" then separate date and time like below
$date = Carbon::parse($timestamp)->format('Y-m-d');
$time = Carbon::parse($timestamp)->toTimeString();
Now you got date and time separated and write query to desire model like below:
User::where('status', 1)->whereDate('created_at', '>=', $date)
->whereTime('created_at', '>', $time)->get();
Hope this will help you. This works for me

Laravel collection with multiple date filter with or operator

I'm trying to filter several items from a Collection in Laravel 5.7. The items have a startdate and an (optional) enddate.
The filter i'm trying to create is the following.
startdate <= now() AND
( enddate >= now() OR enddate = '' OR enddate = NULL )
I've tried the following but it doesn't work:
$this->items->where([
['startdate', '<=', date('Y-m-d'))],
['enddate', '>=', date('Y-m-d')]
])->orWhere([
['startdate', '<=', date('Y-m-d'))],
['enddate', '=', '']
])->orWhere([
['startdate', '<=', date('Y-m-d'))],
['enddate', '=', null]
]);
More cleaner way would be using closure like this:
$this->items->where('startdate', '<=', date('Y-m-d'))
->where(function($q) {
$q->where('enddate', '>=', date('Y-m-d'))
->orWhere('enddate', '')
->orWhereNull('enddate');
});
And probably as suggested in comment you should in fact use items() instead of items because using items you probably get elements from database instead of adding constraints to database query before.
Thanks to Marcin Nabialek i've got the solution.
I've had to use $this->items() instead of $this->items and had to append it with the ->get() function.
The result:
$this->items()->where('startdate', '<=', date('Y-m-d'))
->where(function($q) {
$q->where('enddate', '>=', date('Y-m-d'))
->orWhere('enddate', '')
->orWhereNull('enddate');
})->get();

Laravel between dates

I want to check the validity of promo code by comparing date in eloquent but dont know why its not working
$promoCode = self::where('code', $code)
->where('start_date', '>=', Carbon::now()->toDateString())
->where('end_date', '<=', Carbon::now()->toDateString())
->first();
If i comment out 2nd and 3rd line then it works fine. MySQL column type is date. I also tried whereDate method and thats not working too
It looks like you inverted the signs. You are looking for something that is older than start_date and younger than end_date.
Shouldn't the start_date be before now. If so now should be between. Try:
$promoCode = self::where('code', $code)
->where('start_date', '<=', Carbon::now()->toDateString())
->where('end_date', '<=', Carbon::now()->toDateString())
->first();

Laravel - Database query on default null date

I was trying to get some records of a table, which has a column 'UpdateDate' with default value NULL. I need to check if a record exists that either 'UpdateDate' is not today or 'UpdateDate' is NULL. The query I built is as follows:
DB::table('Users')
->where('id', '=', '10')
->where(function($query) {
$query->where('UpdateDate','<>',date("Y-m-d"))->orWhere('UpdateDate','is','NULL'); })
->exists()
It didn't work as expected. How should I modify it? Thanks.
Change you query to this
DB::table('Users')
->where('id', '=', '10')
->where(function($query) {
$query->where('UpdateDate','<>', date("Y-m-d"))
->orWhereNull('UpdateDate');
})
->exists();
If you have UpdateDate date as datatype.
If you have timestamp or datetime as datatype then change to this
DB::table('Users')
->where('id', '=', '10')
->where(function($query) {
$query->whereBetween('UpdateDate',[date("Y-m-d 00:00:00"), date("Y-m-d 23:59:59")])
->orWhereNull('UpdateDate');
})
->exists();

Resources