使用原始查询 laravel 获取数据

我不知道如何使用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;

}


湖上湖
浏览 84回答 1
1回答

慕后森

你为什么不这样写你的查询呢?public function getOrders(){&nbsp; &nbsp; $order = Order::with([&nbsp; &nbsp; &nbsp; &nbsp; 'menu:id,name,image_url,created_by',&nbsp; &nbsp; &nbsp; &nbsp; 'created_user:id,name,email,avatar',&nbsp; &nbsp; &nbsp; &nbsp; 'menu.created_user:id,name,email,avatar',&nbsp; &nbsp; &nbsp; &nbsp; 'menu.categories'&nbsp; &nbsp; ])&nbsp; &nbsp; &nbsp; &nbsp; ->where(function ($query) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $query->whereNull('start_at')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->orWhere('start_at', '<', DB::raw('NOW()'));&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; ->where(function ($query) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $query->whereNull('end_at')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->orWhere('end_at', '>', DB::raw('NOW()'));&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; ->get();&nbsp; &nbsp; return $order;}
打开App,查看更多内容
随时随地看视频慕课网APP