How To Get Search Query From Multiple Columns in Database

让人想犯罪 __ 提交于 2019-12-06 01:45:49

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();

I hope it helps you.

Alexey Mezenin

You can execute conditional queries in many ways.

1. You can use when():

Book::when($keyword, function ($q) use ($keyword) {
        return $q->where('judul', 'LIKE', '%' . $keyword . '%');;
    })
    ->get();

2. Use the where closure:

Book::where(function($q) use ($keyword, $request) {
        if ($request) {
            $q->where('judul', 'LIKE', '%' . $keyword . '%');
        }
    })
    ->get();

3. Do this:

$books = Book::query();

if ($request) {
    $books = $books->where('judul', 'LIKE', '%' . $keyword . '%');
}

$books = $books->get();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!