laravel 多条件搜索语句
xuexi 2026-01-17 12:48:57 发表在:PHP 查看数:59

laravel 多条件搜索语句

public function search(Request $request)
    {

$keyword = $request->q;
$results = DB::table('cb_shebei_neicun')
    // 核心新增:必选条件 - shebei_id 为 null(未被占用的内存)
    ->whereNull('shebei_id')
    // 分组包裹 OR 条件,避免与 shebei_id 条件冲突
    ->where(function($query) use ($keyword) {
        $query->where('serial', 'like', "%{$keyword}%")
              ->orWhere('manufacturer', 'like', "%{$keyword}%")
              ->orWhere('capacity', 'like', "%{$keyword}%");
    })
    ->get()
    ->toArray();
   $data = [
    'success' => true,
    'data' => [
      "items"=>$results
         ], // 将查询结果放在 data 字段中,结构更清晰
    "total" => 25,    // 内存池总条数
    "page" => 1,      // 当前页码
    "limit" => 8      // 每页条数
];


  return response()->json($data,200);  
    }

最近访问时间:2026-01-24 21:51:10
知识库:414条鸣谢:TAY  备案号:蜀ICP备2024090044号-1