Sql 日期比较雄辩';其中';方法不';行不通

Sql 日期比较雄辩';其中';方法不';行不通,sql,laravel,eloquent,php-carbon,Sql,Laravel,Eloquent,Php Carbon,我正在拼命尝试对SQL请求进行排序: 我想让date属性小于实际日期的每一行 以下是我正在做的: $student = User::find($id)->student; $extras = $student->extras->where('find', 1) ->where('date', '<', Carbon::now()->toDateString()); 如您所见,日期远小于返回此值的Carbon

我正在拼命尝试对SQL请求进行排序:

我想让date属性小于实际日期的每一行

以下是我正在做的:

$student = User::find($id)->student;
$extras = $student->extras->where('find', 1)
                          ->where('date', '<', Carbon::now()->toDateString());
如您所见,日期远小于返回此值的
Carbon::now()->toDateString()

2016-08-19

你知道我做错了什么吗?

你试图用字符串来比较日期。请尝试以下方法:

->where('date', '<', Carbon::now());

->where('date',”请记住,当您使用属性(例如
$student->extras
)访问关系时,结果已经从数据库中提取出来,并且您可以使用
illights\database\Eloquent\Collection
类筛选数据


如果您想让数据库查询结果,您需要使用关系的方法。
$student->extras()->where('find',1)->where('date',谢谢您,先生!$student->extras()->where('find',1)->where('date',刚刚发现了
->get()
,并添加了:)
->where('date', '<', Carbon::now());
$extras = $student->extras->filter(function($extra)
{
    return $extra->date->lt(Carbon::now());
});