如何从数据库中的多个列获取搜索查询 [英] How To Get Search Query From Multiple Columns in Database

查看:128
本文介绍了如何从数据库中的多个列获取搜索查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有搜索表格可以从名为books的表中获取信息.

I have search form to get information from table named books.

现在我正在使用此控制器

Right now i'm using this controller

public function search(Request $request)
{
    $keyword = $request->input('keyword');
    $query = Book::where('judul', 'LIKE', '%' . $keyword . '%');

    $book_list = $query->paginate(5);
    $pagination = $book_list->appends($request->except('page'));
    $total_book = $book_list->total();
    return view('dashboards.index', compact('book_list', 'keyword', 'pagination', 'total_book'));
}

问题是我从请求中获得的数据仅适用于 judul .如果输入关键字搜索指向搜索编写者或发布者,则只会显示空结果

The problem is the data that i get from the request only available for judul. it just show empty result if the input keyword search addressed to search writter or publisher

我希望搜索表单能够从其他名为 writters publisher

I want the search form able to get data from other columns named writters and publisher

是否有任何方法可以从多列中获取数据?

Is there any method to get data from multiple column?

推荐答案

您可以使用orwhere来填充它,就像这样

You can use orwhere to fullfill this, like this

Book::where(function ($query) use($keyword) {
        $query->where('judul', 'like', '%' . $keyword . '%')
           ->orWhere('writters', 'like', '%' . $keyword . '%');
      })
->get();

希望对您有帮助.

这篇关于如何从数据库中的多个列获取搜索查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆