如何在 laravel 中的列的任何字段中查找匹配搜索的数据

How to find data matching the search in any field of the column in laravel

我正在尝试从一个名为 item 的 table 获取数据,我想带上包含用户搜索的搜索字符串的项目,例如用户搜索“active”,所以它会去table 拾取字段中包含“活动”一词的任何数据。我试着用这段代码来做,但它不起作用。

Route::get('/data', function () {
$items = Item::where('status', 'Like', 'active')->get();
dd($items); });

有人有解决办法吗?

您可以使用

$items = Item::where('status', 'like', '%active%')->get();

要搜索多个列,您可以像下面那样orwhere

 $items = Item::where('status', 'like', '%active%')
->orWhere('column2', 'like', '%active%')
->orWhere('column3', 'like', '%active%')
->get();

$columns=['col1','col2','col3'];

 $items = Item::query();
foreach($columns as $column){
$items->orWhere($column,'like','%active%')
}
$result=$items->get();

 $columns=['col1','col2','col3'];
    
     $items = Item::where(function($query)use($columns){
 foreach($columns as $column){
   $query->orWhere($column,'like','%active%')
    }
})->get();