Laravel 过滤所有列中的值

2021-12-26 00:00:00 php laravel eloquent
public function getBooks($input)
{
    $books= Book::where('book_name', 'LIKE', '%' . $input . '%')->get();
    return Response::json($books);
}

我知道如何按给定值过滤列.但是如何按给定值过滤所有列.例如,我有一个名为类别"的列,用户应该能够使用相同的搜索栏来过滤类别.

I know how to filter a column by a given value. But how do I filter ALL columns by a given value. For example, I have a column called 'category' where user should be able to use the same search bar to filter the category.

类似于:

$books = Book::where('all_columns', 'LIKE', '%' . $input . '%')->get();

谢谢!

推荐答案

大多数数据库不支持同时搜索所有列.恐怕您可能不得不将所有列链接在一​​起:

Most databases do not support searching all columns simultaneously. I'm afraid you'll likely have to chain all of the columns together:

$books = Book::where('book_name', 'LIKE', '%' . $input . '%')
    ->orWhere('another_column', 'LIKE', '%' . $input . '%')
    // etc
    ->get();

相关文章