Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

croftCoder's avatar

Laravel Group Results By Days of the Week

i have a database of members with their birthdays stored as date in this format: Y-m-d. e.g 2002-01-15. i want to query the database and group the members by the days of the week from their birthdays like so: members/day-borns/friday should return all those born on friday and memers/day-borns should return all grouped by the days of the week.

any help? how do i go about it?

0 likes
3 replies
tykus's avatar

mysql (if you're using it) has a DAYNAME(date) function so you can constrain your query like this:

Member::whereRaw("DAYNAME(birthday) = 'Friday'")->get()
croftCoder's avatar

@tykus it works, but i need to generate a array/collection grouped by day name also.

tykus's avatar

@croftCoder everyone's birthday?

Member::selectRaw("*, DAYNAME(birthday) as birth_dow")
    ->get() // Collection
    ->groupBy('birth_dow');

Please or to participate in this conversation.