Search code examples
phpsonata

Function to check for all the files active this year


I am getting an error with my PHP function on the sonata framework

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(),
    ]);
}

This is an error i get when testing my code

Error: Call to undefined method


Solution

  • You created the variable $now as a Carbon DateTime, so you dont need the Carbon:: in front of it when you use it.

    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 :)