伙计们,我正在为管理员编写一个筛选器,以查看有关订单的交易历史记录。
搜索表有3个输入。我添加了3 where()查询过滤器...
但不起作用。
如果搜索值存在通过表单提交的内容并且不为null,如何将where query
添加到查询中?
public function search(Request $request){
//dd($request->all());
$order_id = $request->get('order_id');
$transaction_id = $request->get('transaction_id');
$tracking_code = $request->get('tracking_code');
$orderHistories = DB::table('gateway_transactions')
->leftJoin("order_transactions", "gateway_transactions.id", "=", "order_transactions.transaction_id")
->leftJoin("orders", "order_transactions.order_id", "=", "orders.id")
->leftJoin("gateway_transactions_logs", "gateway_transactions.id", "=", "gateway_transactions_logs.transaction_id")
->where('orders.id', $order_id)->where('gateway_transactions.id', $transaction_id)->where('tracking_code', $tracking_code)->paginate(10);
答案 0 :(得分:1)
您应该首先检查值是否为空:
$query = DB::table('gateway_transactions')
->leftJoin("order_transactions", "gateway_transactions.id", "=", "order_transactions.transaction_id")
->leftJoin("orders", "order_transactions.order_id", "=", "orders.id")
->leftJoin("gateway_transactions_logs", "gateway_transactions.id", "=", "gateway_transactions_logs.transaction_id");
if($order_id)
{
$query->where('orders.id', $order_id);
}
if($transaction_id)
{
$query->where('gateway_transactions.id', $transaction_id);
}
if($tracking_code)
{
$query->where('tracking_code', $tracking_code);
}
$query->paginate(10);