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

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

问题描述

我有搜索表单可以从名为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

我希望搜索表单能够从名为 writterspublisher

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天全站免登陆