0

I am very new in Laravel, I try to search with one column and this is my code. But now I want to search with multiple columns - how can I do that?

My controller :

public function search_pers(Request $request)
{
    $mle = $request->input('mle');
    $listpers = Personne::where('mle', 'LIKE', '%'.$mle.'%')->get();

    return view('frontend.etat_civil', ['personnes' => $listpers]);
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Does this help?: [How to Create Multiple Where Clause Query Using Laravel Eloquent?](https://stackoverflow.com/questions/19325312/how-to-create-multiple-where-clause-query-using-laravel-eloquent) – Paul Spiegel Jun 14 '20 at 15:28
  • it doesn't help me – Sara Alaoui Jun 14 '20 at 15:58
  • How about this?: [How To Get Search Query From Multiple Columns in Database](https://stackoverflow.com/questions/48089966/how-to-get-search-query-from-multiple-columns-in-database) – Paul Spiegel Jun 14 '20 at 16:01

2 Answers2

1

You could use an array

     Personne::where([
        ['col1', '=', 'value1'],
        ['col2', '<>', 'value2'],
        [Your_col, OPERATOR, value],
        ...
    ])

or in your case

      $str = "concat('%',".$mle .",'%')";
      $listpers = Personne::where([
            ['mle', 'LIKE', $str],
            ['col1', '=', 'value1'],
            ['col2', '<>', 'value2'],
            [Your_col, OPERATOR, value],
      )->get();
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
0

As I understand what you need is an orWhere condition:

 public function search_pers(Request $request)
    {
      $mle = $request->input('mle');
      $listpers = Personne::where('mle', 'LIKE', '%'.$mle.'%')->orWhere('nom','like','%'.$mle.'%')->get();

      return view('frontend.etat_civil', ['personnes' => $listpers]);
    }

Another alternative and probably a better one is to use fulltext search. MySQL Fulltext search

victoryoalli
  • 1,897
  • 2
  • 12
  • 11