You will need to order by raw and manually write a case statement https://stackoverflow.com/a/39941031
But wouldn't it be easier to just remove the featured flag when the expiry date is lesser than the present date. (scheduled job)?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I need to show listings sorted by the responses_count but need featured listings on the top. 'featured' is a field in the listings table with a date of expiry of the featured facility. So need to consider featured only if the expiry date is greater than the present date.
Controller
$listingsforsale = Listings::where('project_id', '<>', NULL)
->where('expiry_date','>' , now())
->where('listingtype', 1)
->where('dealstatus',1)
->where('status',1)
->withCount('response')
->orderBy('response_count', 'desc')
->orderBy('featured', 'DESC')->latest()
->paginate(10);
How to do that ?
@FounderStartup first write a command that does it. Then add that command to a schedule. https://laravel.com/docs/9.x/scheduling#defining-schedules It could run every hour perhaps?
Please or to participate in this conversation.