dragon87836215 2015-01-12 20:18
浏览 30
已采纳

未定义的变量多个查询范围Laravel

This work perfect:

public function scopeHBO($query)
{
    return $query ->where('network', '=', "hbo");

}

Call in Controller: It Works!

$events = Schedule::HBO()->orderBy('searchdate')->get();

When I add another Query Scope like so:

 public function scopeHBO($query)
{
    return $query
            ->where('network', '=', "hbo")
            ->where('searchdate', '>=', 'NOW()');
}

OR:

public function scopeDate($query)
{
    return $query->where('searchdate', '>= ', 'NOW()');
}

Then call in the controller:

$events = Schedule::HBO()->Date()->orderBy('searchdate')->get();

I get an error: Undefined variable: event. I tried with with Raw MySql in the same model and it works. Whenever i add a query scope, does not matter what it is.. i get that same error Undefined variable: event.

  • 写回答

2条回答 默认 最新

  • doudouwen2763 2015-01-12 20:24
    关注

    NOW() is a function, so you need to use a raw query:

    where('searchdate', '>=', DB::raw('NOW()'))
    

    Then you can use the scopes. (Do note that I think scopeDate must be called as date(), not Date() - not 100 % sure on that though.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?