Search query for multiple search terms

I'm running Laravel 5 and have built a small search function:

$q = Input::get('q'); $search_terms = explode(' ', $q); $user_query = User::select(); $news_query = Article::select(); foreach ($search_terms as $term) { $user_query->where('username', 'like', '%' . $term . '%'); $news_query->where('title', 'like', '%' . $term . '%'); } $user_results = $user_query->get(); $news_results = $news_query->get(); return view('search', ['q' => $q, 'user_results' => $user_results, 'news_results' => $news_results]);

It works for one search term, but doesn't quite work with multiple words.

Example:

  • "boss" returns users and news items that contain "boss"
  • "boss man" returns users and news items that contain "boss"
  • "man boss" returns users and news items that contain "man"

How can I make adjustments so it will return users and news items that contain "boss" or "man" ?

-------------Problems Reply------------

Right now, your query is executed using and statements, so MySQL is looking for usernames consisting of both boss and man.

You can change to orWhere() and it should work right away. Here's a quick experiment I did in Tinker using your code:

$terms = explode(' ', 'c a');
$user_query = User::select();
foreach ($terms as $term) {
$user_query->orWhere('username', 'like', '%' . $term . '%');
}
$user_results = $user_query->get();
$user_results->toArray();

// array(
// 0 => array(
// 'id' => 2,
// 'username' => 'christopher',
// 'created_at' => '2014-04-27 18:41:56',
// 'updated_at' => '2014-11-07 13:42:58',
// 'remember_token' => NULL
// ),
// 1 => array(
// 'id' => 4,
// 'username' => 'Kalle',
// 'created_at' => '2014-11-07 13:42:55',
// 'updated_at' => '2014-11-07 13:42:55',
// 'remember_token' => NULL
// )
// )

Category:php Views:7 Time:2018-07-11

Related post

  • sql search query for multiple optional parameters 2008-12-02

    I'm trying to write a query for an advanced search page on my document archiving system. I'm attempting to search by multiple optional parameters. I have about 5 parameters that could be empty strings or search strings. I know I shouldn't have to che

  • SQL Search Query for Multiple Where Clauses e.g. Firstname AND Surname 2011-01-29

    I have a SQL Search Query which works if I search on the First Name or the Surname: SELECT MemberID, FirstName, Surname, Title + ' ' + FirstName + ' ' + Surname AS FullName FROM Members WHERE FirstName + Surname LIKE N'%' + 'Smith' + '%' ORDER BY Ful

  • How to write search query with multiple filter inputs in asp.net web form for ms sql 2012-02-08

    I need to write a SQL Query for Advance search form shown as Image below. I wrote this partial sql query but i cant make it work correctly as it also get syntax error if user select second check-box and leaves the first check-box. strSql = "SELECT Ar

  • Search Folder for Multiple Inboxes 2015-01-25

    I work in an enterprise environment with multiple Exchange servers. I have my work Inbox, and I also monitor 4 other inboxes for system changes, security notifications, VPN requests, and virus reports. These are separate accounts on the Exchange serv

  • Adwords API BulkMutateJobService Fetch Global Monthly Search Volume For Multiple Keywords 2011-02-04

    I've just gotten into the Adwords API for an upcoming project and I need something quite simple actually, but I want to go about it the most efficient way. I need code to retrieve the Global Monthly Search Volume for multiple keywords (in the million

  • SQL Complex Search Query With Multiple Value 2012-04-27

    I'm trying to achieve something that Im not sure how to do it. I'm working on a website based on ASP Classic and SQL 2008, IIS7. In search page I have option to have couple value per query like default.asp?q=one|two|three&q2=four|five|six . What

  • Where can I get detailed information about using Windows 7 search syntax for compound searches? 2013-12-24

    I have read the information at http://www.microsoft.com/windows/products/winfamily/desktopsearch/technicalresources/advquery.mspx (several times) and have found it most unhelpful. I have about 4000 PDF documents and photos with metadata -- Title, sub

  • SQL query for multiple tag inclusion 2010-09-26

    I´m net very good in explaining this. But i do my best: I've made a tagging tool for people. Some people contains more then 1 tag, and i need to get those but dont know how to write the SQL query for multiple tag inclusion. I know you can't write WHE

  • InfoPath: Query for multiple values in one query field 2011-03-21

    I have a form built in InfoPath 2010 that has multiple check box fields for querying with predetermined values. The problem I am running into though is that I cannot seem to query for multiple values. Example: I'd like to query for String 1 and Strin

  • How do I create my own search provider for Instant Search in IE8? 2014-05-17

    IE7 in Vista allowed me to create my own search providers for Instant Search. It was soooo easy, and the personalized search providers made my lookups at work so much easier. From start to finish it took me about 10 seconds to create a new search pro

  • MySQL query for ALL search terms in index 2009-08-16

    I have a table of employees and their schedule, like so: Emp_Name | Date -------- ----- Smith | 08-01-2009 Jones | 08-01-2009 Goodman | 08-02-2009 Smith | 08-02-2009 Jones | 08-02-2009 Goodman | 08-03-2009 How would I write a query so that the result

  • RavenDB: How to query with multiple search terms 2011-10-26

    My entity is: class Resource { string Name; string EmployeeId; } How do I query for resources of multiple employees? I tried this: Resource[] FindResourcesByEmployees(string[] employeeIds) { return this.Session.Query<Resource>() .Where(r =>

  • data structures and search algorithm for multiple predicate 2010-07-30

    Does anyone know any good data structure and algorithm for searching with multiple predicate. eg. suppose I have a set of tcp header data (assuming no duplicate). If I were searching for an tcp header of the list by src ip, I could sort the set by sr

  • Search strategy for multiple fields in web app 2010-09-02

    Got a web app in development; requirement is to provide a single search field which searches important fields in main table plus other fields related by PK/FK from joined tables Schema goes something like this PROJECTS projectID projectTitle projectT

  • SQL Server Full Text Search using CONTAINS, FORMSOF, NEAR for multiple search words 2011-01-18

    I am new to SQL Server Full Text Searching, and am trying to figure out the best way to search on multiple words using the inflectional engine so the search uses the various forms of all of the words. From what I read, FREETEXT uses an implicit OR wh

  • How to write a search query for this form 2009-10-27

    I want to write a search query that search on the criteria like city, rent, area. i am new to database and sql queries. How to write query. Please help. Any suggestions will be highly appreciated. My form snapshot is here ->> --------------Solu

  • Mysql - Help me alter this search query involving multiple joins and conditions to get the desired results 2010-06-02

    About the system: We are following tags based search. Tutors create packs - tag relations for tutors stored in tutors_tag_relations and those for packs stored in learning_packs_tag_relations. All tags are stored in tags table. The system has 6 tables

  • Search Query across multiple fields including JOIN 2011-07-14

    Put simply, I want to run a search query on my database but i'm a little lost on how to appraoch it. To start with, the search is not an open text field for the user to input anything they like, but is made up of three select menus of set fields and

  • How can I show multiple areas of search suggestions for Android search suggestions? 2012-02-09

    I have an iPhone app with search suggestions that divides the search suggestions into "your query text appears in the name" and "your query text appears somewhere else in the description". The thinking is that users are most likely to be searching by

  • Performance for multiple searches ordered by random 2012-02-28

    I'm in the design phase of a website and I have a solution for a feature but I don't know if it will be the good one when the site, hopefully, grows. I want the users to be able to perform searches for other users and the results they find must be or

  • SQL Query for retrieving search results 2009-05-29

    I need help designing a query that will be used to return a website's search results. Users search by selecting items from a list of attributes. Each search result item returned must have ALL the attributes selected by the user. The challenge (for me

  • Query for a search form 2009-07-20

    I'm developing a search form. It has about 10 fields and a search button that retrieve records from a table and put them in a gridview. The user can fill some fields, all the fields or none, and then click Search. Only the fields with info has to be

  • How can I solve this simple search query for php MySQL 2011-03-12

    I have a MySQL table (table1) with following fields... id, title, description, detail, category, status What I am trying to do is searching these fields with regular php search query... My query for now is... $q = mysql_query("SELECT * FROM table1 WH

  • best lucene query for user search input 2011-03-23

    I have news storage with 1,000,000 records Im using lucene library for fulltext searching my news hast (title, body, news date, ...) i need to find the best query for finding most relevant results from user input what strategy or algorithm i should u

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.166 (s). 11 q(s)