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

ok4mee's avatar

How can I match on date months in laravel?

Hi! I'm making my first laravel project, using postgres, and I'd like to be able to access all the people with a birthday this month (my people table has a birthdate field that's a date). I can use extract to get these records from the database, like so:

select * from people where extract (month from birthdate) = 11;

But when I try a few different ways in my controller I get 'unknown column' errors:

$birthday_people = DB::table('people') ->where ("extract(month from birthdate)", "=", "11") ->get();

(I'll ultimately adjust it to compare with Carbon::now()->month, and use the model Person::all(), but until I get some results coming through I'm going as simple as possible)

Is there a special way to get extract to work in laravel? Or is there a different way to do this all together? Any help is greatly appreciated!

0 likes
7 replies
Harish's avatar

Use WhereMonth in the scope query Eg code :

 public function scopeBirthdays($query)
 {
   return $query-> User::whereMonth('DOB' , Carbon::today()->month);
4 likes
toniperic's avatar
Level 30

@ok4mee

$data = DB::table('people')->whereRaw('extract(month from birthdate) = ?', ['11'])->get();
4 likes
ok4mee's avatar

Thanks guys!

@Harish I like the scope idea! It's working when I use a where clause for a specific example: return $query->where('birthdate', '=', '1947-11-09');

But when I update it to use whereMonth I have issues: it gave me an 'undefined function' error until I put the bool on the end, and now the current error suggests that it's interpretting number of months instead of which one(?):

public function scopeBirthdays($query) { return $query->whereMonth('birthdate', '=', Carbon::today()->month, true); }

I get: Syntax error: 7 ERROR: syntax error at or near "month" LINE 1: select * from "people" where 1 month("birthdate") = $1 ^ (SQL: select * from "people" where 1 month("birthdate") = 11)

@Toniperic, I tried your suggestion too- it gives me results, but the downside is they're not being interpretted as 'people' results (I can't make use of scopes & fields & stuff). If I can't get whereMonth to work then this can be a good start as a hack, but we'll see if I can get that one going.... thanks!

Harish's avatar

@ok4mee lets go step by step

  1. In your Modal add the date of birth field to the protected dates array to convert it into a carbon instance then add the following scope query
    public function scopeBirthday($query)
        {
        return $query->whereMonth('DOB','=',Carbon::today()->month);
        }
  1. In your controller pass this to a variable and compact it to your view
    $birthdays = modalName::birthday()->get();
  1. Now access it in your view as

     @foreach($birthdays as $birthday)
                 <td>{{ $birthday->DOB->toFormattedDateString() }}</td>
             @endforeach 
    

Dont forget to add this on the top of your modal , controller and view

    <?php
    use Carbon\Carbon;
    ?>

Hope this helps mate :)

ok4mee's avatar

Thank you! Yeah it's the strangest thing- I've got birthdate as a protected date in my Person model, did everything exactly the same, yet I get this error when I load the view:

SQLSTATE[42883]: Undefined function: 7 ERROR: function month(date) does not exist LINE 1: select * from "people" where month("birthdate") = $1 and "hi... ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts. (SQL: select * from "people" where month("birthdate") = 11 and "hide_bool" = 0)

It's frustrating that I haven't figured out what I'm doing wrong, but the good news is the whereRaw suggestion from @toniperic gets me there- doing my scope like this gets me the results:

public function scopeBirthdays($query)
{
    return $query->whereRaw('extract(month from birthdate) = ?', [Carbon::today()->month])->orderBy ('birthdate', 'asc');

}

Thanks for your help and clear steps- I've been banging my head against the wall so I'm grateful :)

rohitkhatri's avatar

@ok4mee I don't see any month scope in your code

try this if you want to pass month

public function scopeBirthdays($query,$month)
{
  return $query->whereRaw('extract(month from birthdate) = ?',[$month])->orderBy ('birthdate', 'asc');
}

Person::Birthdays(11)->get();

Or if you want to get birthdays in current month, without passing month, then it will be like

public function scopeBirthdays($query)
{
  return $query->whereRaw('extract(month from birthdate) = ?', [Carbon::today()->month])->orderBy ('birthdate', 'asc');
}

and you call is like this

Person::Birthdays()->get();
1 like
ok4mee's avatar

Thanks @rohitkhatri! Yes- your second suggestion matches what I did, since I'll just be getting them for the current month, rather than getting them for a given month.

1 like

Please or to participate in this conversation.