Well, the format is the only difference, so
->selectRaw('DATE_FORMAT(created_at, $boolean ? "%M-%y" : "%Y-%m-%d") as date')
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have two queries based on some value, they are nearly identical so I feel like I can do it neater. The only difference in the query is this part:
->select(DB::raw('DATE_FORMAT(created_at, "%M-%y") as date'))
And
->select(DB::raw('DATE(created_at) as date'))
So right now I have an if-else block based on some $value
Can I remove the condition and make it same query and in a still readable way? Or any change, such as doing the condition and echo the query inline will make it worse
Well, the format is the only difference, so
->selectRaw('DATE_FORMAT(created_at, $boolean ? "%M-%y" : "%Y-%m-%d") as date')
Please or to participate in this conversation.