检查今年所有活动文件的功能

Function to check for all the files active this year

我在 sonata 框架

上的 PHP 函数出错
public function getIsActiveThisYear(): bool
{
    $now = Carbon::now();
    return $this->whereBetween($endDate, [
        Carbon::$now->startOfYear(),
        Carbon::$now->endOfYear(),
    ])|| $this->whereBetween($endDate, [
        Carbon::$now->startOfYear(),
        Carbon::$now->endOfYear(),
    ]);
}

这是我在测试代码时遇到的错误

Error: Call to undefined method

您将变量 $now 创建为 Carbon DateTime,因此您在使用它时不需要前面的 Carbon::

public function getIsActiveThisYear(): bool
{
    $now = Carbon::now();
    return $this->whereBetween($endDate, [
        $now->startOfYear(),
        $now->endOfYear(),
    ])|| $this->whereBetween($endDate, [
        $now->startOfYear(),
        $now->endOfYear(),
    ]);
}

Also you use $this->whereBetween($endDate, twice, maybe one should be a start Date?? But thats a bit of a guess :)