Reputation: 133
I have tasks table along with date columns 'reminder date' and 'due date'. I need to get all tasks in between those days referencing today. I have little knowledge in query so ORM will be better.
I tried,
$today = date("Y-m-d");
$availTasks = Task::whereRaw($today.' between rem_date and due_date')->get();
echo ($availTasks);
Upvotes: 0
Views: 2358
Reputation: 2520
How about a compound where using a closure:
$dateToday = date('Y-m-d H:i:s');
$query = Task::where(function ($query) use ($dateToday){
$query->where('rem_date', '>=', $dateToday);
$query->where('due_date', '<', $dateToday);
});
echo $query->get();
Also when using whereRaw you should always bind your parameters as it can become an injection vector. I know that the parameter is just the date in your case, but it's just good practice. See mr. Nabiałek answer about this.
Upvotes: 2
Reputation: 111889
First, to make your date string used correctly it should be:
$availTasks = Task::whereRaw('? between rem_date and due_date', [$today])->get();
if your rem_date
and due_date
columns are DATE
type it should work. However if they are DATETIME
type it won't work. You should use then:
$availTasks = Task::whereRaw('? between DATE(rem_date) and DATE(due_date)', [$today])->get();
Upvotes: 3