how whereBetween handle to this query - laravel

I want to use whereBetween to handle this query
select * from schedules where now() BETWEEN start and end
thank for your attentions

You can do like this
$schedules = DB::table('schedule')->select('id', 'name')
->whereBetween( DB::raw('now()'), [$startDate, $endDate])
->get();

You can write this query as:
$schedules = DB::table('schedules ')->select( 'id','name')
->where( DB::raw('now()'), '>=', 'startDateField' )
->where( DB::raw('now()'), '<=', 'endDateField' )
->get();
Also as per comment by #Devon, you can use whereBetween:
$schedules = DB::table('schedules ')->select( 'id','name')
->whereBetween( DB::raw('now()'), array('startDateField', 'endDateField')
->get();

Related

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();

Why won't my Laravel collection filter correctly by date?

I have
$dt = Carbon::now();
$b4 = Carbon::now()->addWeeks(12);
Event::orderBy('date', 'desc')
->where('date', '<', $b4)
->where('ends', '>=', $dt)
->orWhere('date', '>=', $dt)
->take(3)
->get();
All filters work except the first where. I tried it following the orWhere first, no dice. It displays items with the date in August 2018. Help?
I think you need to group your where queries ie.
$dt = Carbon::now();
$b4 = Carbon::now()->addWeeks(12);
Event::orderBy('date', 'desc')
->where(function ($query) use ($dt, $b4) {
$query->where('date', '<', $b4->format('Y-m-d'))
->where('ends', '>=', $dt->format('Y-m-d'));
})
->orWhere('date', '>=', $dt->format('Y-m-d'))
->take(3)
->get();
https://laravel.com/docs/5.5/queries#parameter-grouping

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();

laravel join query is not working

I have the next query and I want to get the price_types.name but is not returned:
$projects = Project::with('projectsTask')
->select('projects.*',
'price_types.name as name_type'
)
->where('client_id', $client->id)
->join('price_types', 'tasks.type_list', '=', 'price_types.id')
->orderBy('id')
->get();
Here an image query is retrievng
This on picture "type_list" must be string text
Maybe somebody can help me.
Many thanks!
Try this:
$projects = Project::with('projectsTask')
->where('client_id', $client->id)
->join('price_types', 'tasks.type_list', '=', 'price_types.id')
->orderBy('id')
->get([''projects.*',
'price_types.name as name_type'']);
get method receive as parameter an array with fields that you want.
$projects = Project::join('tasks', 'projects.id', '=', 'tasks.project_id')
->select('tasks.*',
'price_types.name as name_type',
'statuses.name as name_status'
)
->where([['client_id', $client->id], ['tasks.status_type', '!=', 2]])
->join('price_types', 'tasks.type_list', '=', 'price_types.id')
->join('statuses', 'tasks.status_type', '=', 'statuses.type')
->orderBy('tasks.id', 'DESC')
->get();

Resources