我有一個如下查詢。當我使用下面的查詢進行搜索時,它會獲取產品,但即使我指定了航空公司 ID,它也會顯示來自其他航空公司的其他產品。當我拿出它時orWhere("code","LIKE","%{$request->search}%"),它作業得很好,并且沒有顯示其他航空公司的產品。
我知道問題出在哪里,但我仍然需要搜索引數是名稱或代碼。我該如何解決 ?
Product::with('airline')
->where('airline_id', $request->airline_id)
->select('id','name','code')
->where("name","LIKE","%{$request->search}%")
->orWhere("code","LIKE","%{$request->search}%")
->limit(5)
->get();
uj5u.com熱心網友回復:
您當前的查詢將生成:
WHERE airline_id = ? AND name LIKE ? OR code LIKE ?
除非您將OR它們正確分組,否則它將打折任何其他條件。您需要使用邏輯分組來解決這個問題:
->select('id','name','code')
->where('airline_id', $request->airline_id)
->where(function ($query) use ($request) {
$query->where("name","LIKE","%{$request->search}%")
->orWhere("code","LIKE","%{$request->search}%");
})
這將產生:
WHERE airline_id = ? AND (name LIKE ? OR code LIKE ?)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/427348.html
