Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
674 views
in Technique[技术] by (71.8m points)

laravel - How To Get Search Query From Multiple Columns in Database

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'));
}

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...