Sara Alaoui
Sara Alaoui

Reputation: 5

How can I search with multiple columns in Laravel

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]);
}

Upvotes: 0

Views: 2760

Answers (2)

victoryoalli
victoryoalli

Reputation: 2389

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

Upvotes: 0

ScaisEdge
ScaisEdge

Reputation: 133360

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();

Upvotes: 1

Related Questions