ctr*_* f5 2 php laravel laravel-5
我很困惑,真的不知道该如何选择在哪两个地方使用?
我读了两个文档
https://laravel.com/docs/5.4/queries#where-clauses
和
https://laravel.com/docs/5.4/queries#raw-expressions
如果我使用类似这样的查询,则无法正常工作
DB::table('table_name')
->where('parent_id', $parent_id)
->whereRaw("date",">",$date)
->get();
但是有效
DB::table('table_name')
->where('parent_id', $parent_id)
->where(DB::raw("date",">",$date))
->get();
DB::raw()使您可以将原始语句编写为查询的一部分。例如:
->where(DB::raw('DATE(date_column)'), '>', '2017-01-01')
但是,如果您需要编写一个完整的“原始位置 ”,则应使用whereRaw(为了您的方便)。例如:
->whereRaw('DATE(date_column) > DATE(another_date_column)')
另外,whereRaw()接受完整的where子句。
因此,在第一个示例中,它不起作用,因为您应该这样做:
->whereRaw("date > ".$date)
whereRaw()就像我在回答中使用上面的语句一样,可以简化您的第二个示例。
也DB::raw()可用于->select(),groupBy()和其他人。
| 归档时间: | 
 | 
| 查看次数: | 2267 次 | 
| 最近记录: |