我如何在 Laravel 中搜索多列

我是 Laravel 的新手,我尝试用一列进行搜索,这是我的代码。但现在我想搜索多列 - 我该怎么做?


我的控制器:


public function search_pers(Request $request)

{

    $mle = $request->input('mle');

    $listpers = Personne::where('mle', 'LIKE', '%'.$mle.'%')->get();


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

}


呼啦一阵风
浏览 141回答 2
2回答

饮歌长啸

你可以使用一个数组&nbsp; &nbsp; &nbsp;Personne::where([&nbsp; &nbsp; &nbsp; &nbsp; ['col1', '=', 'value1'],&nbsp; &nbsp; &nbsp; &nbsp; ['col2', '<>', 'value2'],&nbsp; &nbsp; &nbsp; &nbsp; [Your_col, OPERATOR, value],&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; ])或者你的情况&nbsp; &nbsp; &nbsp; $str = "concat('%',".$mle .",'%')";&nbsp; &nbsp; &nbsp; $listpers = Personne::where([&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ['mle', 'LIKE', $str],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ['col1', '=', 'value1'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ['col2', '<>', 'value2'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [Your_col, OPERATOR, value],&nbsp; &nbsp; &nbsp; )->get();

子衿沉夜

据我了解,您需要的是 orWhere 条件: 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]);    }另一种可能更好的选择是使用全文搜索。 
打开App,查看更多内容
随时随地看视频慕课网APP