我想使用laravel查询生成器创建过滤器。
我有以下代码:
$queries = array();
$paginate = 10;
$orders = DB::table('orders')->whereNull('deleted_at');
// the filter i want to apply
$from = 2018-06-18;
$end = 2018-06-20;
$orders->whereBetween('orders.created_at', array($from, $end));
$queries['dateRange'] = request('dateRange');
// other filters
...
$orders->leftJoin('order_status', 'orders.OrderID', '=', 'order_status.OrderID')
->select('orders.*', 'order_status.Status')
->orderBy('OrderID', 'desc')
->paginate($paginate)
->appends($queries);
问题是我遇到错误-无法正常工作。如果还没有使用leftjoin,我可能会这样使用
($orders = Order::query();).
我如何使用带有某些过滤器的 DB :: table 查询,或者对此有更好的解决方案?
更新
更具体地说:
此代码有效:
$orders = DB::table('orders')->whereNull('deleted_at')
->leftJoin('order_status', 'orders.OrderID', '=', 'order_status.OrderID')
->select('orders.*', 'order_status.Status')
->orderBy('orders.OrderID', 'desc')
->paginate($paginate)
->appends($queries);
此代码不起作用:
$orders = DB::table('orders')->whereNull('deleted_at');
$orders->leftJoin('order_status', 'orders.OrderID', '=', 'order_status.OrderID')
->select('orders.*', 'order_status.Status')
->orderBy('orders.OrderID', 'desc')
->paginate($paginate)
->appends($queries);
答案 0 :(得分:0)
我已更改为这种方法,现在可以使用了。问题是我必须在另一行上指定分页:
$orders = DB::table('orders')->whereNull('deleted_at');
// date filter
$orders->whereBetween(DB::raw('DATE(orders.created_at)'), array($from, $to));
$orders->leftJoin('order_status', 'orders.OrderID', '=', 'order_status.OrderID')
->select('orders.*', 'order_status.Status')
->orderBy('orders.OrderID', 'desc');
// the paginate and appends write on new line :)
$orders = $orders->paginate($paginate)->appends($queries);