Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 490
Yudy Ananda Avatar asked Jan 04 '18 06:01

Yudy Ananda


People also ask

How do I search for multiple columns in SQL?

To select multiple columns from a table, simply separate the column names with commas! For example, this query selects two columns, name and birthdate , from the people table: SELECT name, birthdate FROM people; Sometimes, you may want to select all columns from a table.

How do I subquery with multiple columns?

If you want compare two or more columns. you must write a compound WHERE clause using logical operators Multiple-column subqueries enable you to combine duplicate WHERE conditions into a single WHERE clause.

How do I select multiple columns as single column in SQL?

SELECT COALESCE(column1,'') + COALESCE(column2,'') FROM table1. For this example, if column1 is NULL , then the results of column2 will show up, instead of a simple NULL .

How to search multiple columns in a MySQL database?

How to search multiple columns in MySQL? Note: We assume we have created a database named ‘DBNAME’ and a table named ‘tableName’. The ‘AND’ and ‘OR’ operators can be used, depending on what the user wants the search to return. SELECT colName FROM tableName WHERE my_col LIKE %$param1% AND another_col LIKE %$param2%;

What is an example of multiple columns in a query?

For Example: Write a query that gave the names of EMPLOYEE in an organization, so here we have to pick out only the name column from that particular EMPLOYEE table. Similarly, another example of multiple columns can be: Write a query which gave the names and salary of all employees working in an organization.

How do I search for objects in multiple databases?

You can search for objects in all databases in the connected instance using this object explorer search. On the home page of the object explorer, enter the object name and search. In the result below, you see that a specified object exists in multiple databases. You can browse to the specified object in the database using the object explorer.

How to select any column in SQL?

In SQL, selecting any column is one of the easiest things as you have to type only the SELECT command and after that, the column name and the output will be the desired column. To make it more clear let’s take a general example of the EMPLOYEE table, which we have created above.


1 Answers

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.

like image 59
Ritesh Khatri Avatar answered Sep 21 '22 01:09

Ritesh Khatri