Laravel search query with multiple conditions

后端 未结 3 817
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-31 05:52

Newbie to PHP/Laravel here so please be patient.

I have a webpage that is searching based on 3 criteria for dogs , breed, sex and radius.

here is the relevant co

3条回答
  •  长发绾君心
    2021-01-31 06:04

    Here's one possible solution, I think there are probably others. Create an empty query builder with the query() function and add the non-null clauses to it, then call the paginate() function at the end.

    $builder = Dogs::query();
    if (Input::has('search')) {
        $queryString = Input::get('search');
        $builder->where('name', 'LIKE', "%$queryString%");
    }
    // ... more clauses from the querystring
    $dogs = $builder->orderBy('name')->paginate(5);
    

提交回复
热议问题