That query should work, but you can also use the whereDate()
clause.
$currentDate = date('Y-m-d');
$articles = Article::whereDate('expiration_date', '<=', $currentDate)->get();
You can also use the whereRaw()
clause and get rid of the php date.
$articles = Article::whereRaw('expiration_date <= curdate()')->get();
Thanks thomastkim for your reply with your suggestions. My query was indeed almost correct, but I made a mistake with the comparison operator. Using '<=' would mean that I only get expired records.
To get the records that are not yet expired (with an expiration_date BEFORE the current date), this works:
$currentDate = date('Y-m-d');
$articles = Article::where('expiration_date', '>', $currentDate)->get();
Problem solved!
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community