Laravel Eloquent whereTime() shows null - laravel

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

Related

Get records between two columns using range between two dates, Also search filter criteria

I want to get records that are joined between this date period (2022-02-15, 2022-08-20)
$users = User::whereDate('start_at', '>=', $startDate)
->whereDate('end_at', '<=', $endDate)
->get();
$_start_date = '2022-02-15';
$_end_date ='2022-08-20';
$users = User::where(function ($query) use ($_start_date, $_end_date) {
$query->where(function ($query) use ($_start_date, $_end_date) {
$query->whereRaw("start_date >= date('$_start_date')")
->whereRaw("end_date <= date('$_end_date')");
})
->orwhere(function ($query) use ($_start_date, $_end_date) {
$query->whereRaw("start_date <= date('$_start_date')")
->whereRaw("end_date >= date('$_end_date')");
});
})->get();
try above code this will also return between dates of start_date and end _date e.g below
$_start_date = '2022-02-18';
$_end_date ='2022-08-20';
and also works with below dates e.g
$_start_date = '2022-02-16';
$_end_date ='2022-08-18';
codes look like this in ide
You can use the whereBetween Laravel function:
$users = User::whereBetween('start_at', [ $startDate, $endDate])->get();
Note $startDate and $endDate must be instance of Carbon.
$startDate = new Carbon('paste_your_start_date')->format('Y-m-d')." 00:00:00";
Update
This would be one method to get data from two seperated columns. Example is not tested.
$start_at = Carbon::createFromFormat('Y-m-d H', '2022-02-15 0')->toDateString();
$end_at = Carbon::createFromFormat('Y-m-d H', '2022-08-20 0')->toDateString();
$users = User::whereRaw("start_at <= date('$start_at')")
->whereRaw("end_at >= date('$end_at')")
->get();
Depends on what you need exactly
User::whereDate('start_date', '>=', $startDate)->where('end_date', '<=', $endDate)->get();
or
User::whereDate('start_date', '<', $User)->whereDate('end_date', '>', $startDate)->get();
I think you want to get the records that at least one day from start_date to end_date that exists between 2022-02-15 and 2022-08-20:
$startDate = '2022-02-15';
$endDate = '2022-08-20';
$users = User::whereDate('start_at', '<=', $endDate) // where or whereDate also should work
->whereDate('end_at', '>=', $startDate)
->get();

How to get last 30 days data in laravel?

I am querying data for last 30 days and not getting expected result. Today is 2021-01-18 and last three days date is 2021-01-18 but I am getting only this month and year data not December how can i get?
My code is
$startDate = \Carbon\Carbon::now()->subDays(30);
$endDate = \Carbon\Carbon::now();
return Auth::user()->invoices()
->whereBetween("invoice_date", [$startDate, $endDate])
->orderBy('created_at', 'DESC')
->get();
and also try other solutions like this,
$date = \Carbon\Carbon::now()->subDays(30);
return Auth::user()->invoices()
->whereDate("invoice_date", '>=', $date)
->orderBy('created_at', 'DESC')
->get();
get same output my database table data is,
and display out is
Try below code it will help you:
$date = \Carbon\Carbon::now()->subDays(30);
$year = \Carbon\Carbon::now()->year;
return Auth::user()->invoices()
->where("invoice_date", '>=', $date)
->where("invoice_date", '>=', $year)
->orderBy('created_at', 'DESC')
->get();

Laravel eloquent query convert created_at datetime to date

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;

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 5 - Check if date is today

I'm creating a timeline and I'm nearly finished. I want that the color of the date for each timeline event is the same beside if the date is "today".
So I need something like:
#if($event[$i]->created_at->format('d.m.Y') == *code or variable that says its today*)
....
#endif
But I couldn't figure out what I can do to save the todays date in a variable.. Does anybody knows a solution for this?
thanks!
You can use isToday() method to check if date is today:
if ($event[$i]->created_at->isToday())
you can use Carbon
#if($event[$i]->created_at->format('d.m.Y') == \Carbon::today() )
....
#endif
You can use
whereDate, whereMonth, whereDay, whereYear, whereTime
whereDate()
$users = DB::table('users')
->whereDate('created_at', '2019-11-31')
->get();
whereMonth()
$users = DB::table('users')
->whereMonth('created_at', '10')
->get();
whereDay()
$users = DB::table('users')
->whereDay('created_at', '20')
->get();
whereYear()
$users = DB::table('users')
->whereYear('created_at', '2019')
->get();
whereTime()
$users = DB::table('users')
->whereTime('created_at', '=', '11:20:45')
->get();

Resources