Rob*_*ina 1 php laravel eloquent
public function getBooks($input)
{
$books= Book::where('book_name', 'LIKE', '%' . $input . '%')->get();
return Response::json($books);
}
Run Code Online (Sandbox Code Playgroud)
我知道如何按给定值过滤列.但是如何按给定值过滤所有列.例如,我有一个名为"类别"的列,用户应该可以使用相同的搜索栏来过滤类别.
就像是:
$books = Book::where('all_columns', 'LIKE', '%' . $input . '%')->get();
Run Code Online (Sandbox Code Playgroud)
谢谢!
您必须where为每个列添加一个子句,如@JoelHinz建议的那样。为了简化一点,您可以使用数组和循环:
$query = Book::query();
$columns = ['book_name', 'foo', 'bar'];
foreach($columns as $column){
$query->orWhere($column, 'LIKE', '%' . $input . '%');
}
$books = $query->get();
Run Code Online (Sandbox Code Playgroud)
甚至甚至可以使用架构构建器从表中检索所有列名称:
$columns = Schema::getColumnListing('books');
Run Code Online (Sandbox Code Playgroud)
大多数数据库不支持同时搜索所有列.我担心你可能不得不将所有列连在一起:
$books = Book::where('book_name', 'LIKE', '%' . $input . '%')
->orWhere('another_column', 'LIKE', '%' . $input . '%')
// etc
->get();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4893 次 |
| 最近记录: |