Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 使用laravel/postgres访问日期的月份_Postgresql_Date_Laravel_Eloquent - Fatal编程技术网

Postgresql 使用laravel/postgres访问日期的月份

Postgresql 使用laravel/postgres访问日期的月份,postgresql,date,laravel,eloquent,Postgresql,Date,Laravel,Eloquent,我正在使用postgres制作我的第一个laravel项目,我希望能够访问所有本月有生日的人(我的人表有一个生日字段,它是日期)。我环顾四周,无法找到访问月份并与当前月份进行比较的正确语法 我在控制器中尝试了几种方法: 1) 这里的箭头运算符对于生日->月份是意外的: public function get_birthday_people() { $this_month = \Carbon\Carbon::now()->month; $birthday_people

我正在使用postgres制作我的第一个laravel项目,我希望能够访问所有本月有生日的人(我的人表有一个生日字段,它是日期)。我环顾四周,无法找到访问月份并与当前月份进行比较的正确语法

我在控制器中尝试了几种方法:

1) 这里的箭头运算符对于生日->月份是意外的:

  public function get_birthday_people()
{
    $this_month = \Carbon\Carbon::now()->month;

    $birthday_people = Person::all()
        ->Where('birthdate'->month, $this_month)
        ->Where('active', true)
        ->get();

    return $birthday_people;
}

2) 在这里,我调用了undefined方法illumb\Database\Eloquent\Collection::whereMonth():


这样做的正确方法是什么?

试试这样的方法

 $birthday_people = Person::all()
    ->where("date_trunc('month', birthdate)", "=", Carbon::now()->month)
    ->where("active", true)->get();

Extract等于MySQL MOTNH()或YEAR()等。

谢谢!是的,现在阅读那些postgres datetime函数,提取听起来像我想要的,现在它的有效语法-谢谢!不幸的是,它还不匹配-我没有得到结果,即使有很多人的生日是11月-所以我会继续调查,但这是一个很大的帮助!尝试添加一个虚拟记录,只是为了检查。是的,这绝对是正确的使用方法-当我查询我的数据库时,它可以工作,但在我的laravel控制器中它不能工作。我将引出一个新问题,这样就不会太麻烦了:谢谢你的帮助!
 $birthday_people = Person::all()
    ->where("date_trunc('month', birthdate)", "=", Carbon::now()->month)
    ->where("active", true)->get();
$birthday_people = Person::all()
        ->where("extract(MONTH from birthdate)", "=", Carbon::now()->month)
        ->where("active", true)->get();