【发布时间】:2015-08-31 21:40:08
【问题描述】:
尝试在 laravel 中编写查询:
return DB::table('course_modules')->select('module_id', 'publish_from', 'publish_to')
->where('course_id', $course_id)
->where('is_deleted', '0')
->orwhere('publish_from', '>=', date('Y-m-d H:i:s'))
->orwhere('publish_to', '<=', date('Y-m-d H:i:s'))
->orwhere(function($query) {
$query->where('publish_from', '>=', date('Y-m-d H:i:s'))
->where('publish_to', '<=', date('Y-m-d H:i:s'));
})
->where('hide_module', 1)
->get();
我只想在 publish_to 和 publish_from 字段不为 NULL 时应用 where 子句。
我想这样做:
if(publish_from!=NULL){ ->orwhere('publish_from', '>=', date('Y-m-d H:i:s'))}
同样的方式:
if(publish_to!=NULL){ ->orwhere('publish_to', '>=', date('Y-m-d H:i:s'))}
没有得到确切的方法。帮助...
【问题讨论】:
-
这样使用 where('field', '!=', null)
-
我想这样做: if(publish_from!=NULL){ ->orwhere('publish_from', '>=', date('Y-m-d H:i:s'))} AND相同的方法:f(publish_to!=NULL){ ->orwhere('publish_to', '>=', date('Y-m-d H:i:s'))}
标签: laravel where-clause