我不知道如何使用Order::with原始查询获取数据,我想$order使用Where($query).public function getOrders(){ $order = Order::with([ 'menu:id,name,image_url,created_by', 'created_user:id,name,email,avatar', 'menu.created_user:id,name,email,avatar', 'menu.categories' ])->get(); $query = 'SELECT * FROM orders WHERE (now() >= start_at OR start_at IS NULL) AND (now() <= end_at OR end_at IS NULL)'; $currentOrder = DB::select(DB::raw($query)); return $currentOrder;}
1 回答
慕后森
TA贡献1802条经验 获得超5个赞
你为什么不这样写你的查询呢?
public function getOrders()
{
$order = Order::with([
'menu:id,name,image_url,created_by',
'created_user:id,name,email,avatar',
'menu.created_user:id,name,email,avatar',
'menu.categories'
])
->where(function ($query) {
$query->whereNull('start_at')
->orWhere('start_at', '<', DB::raw('NOW()'));
})
->where(function ($query) {
$query->whereNull('end_at')
->orWhere('end_at', '>', DB::raw('NOW()'));
})
->get();
return $order;
}
- 1 回答
- 0 关注
- 83 浏览
添加回答
举报
0/150
提交
取消