Searching Multiple Rows in database - php

Hello everyone I have ran into a small problem i need to know how to be able to search multiple rows in my database and i am having some difficulty with my code can you please take a look at my query
$raw_results = DB::table('students')->select('Name', 'Date' ,'Class')->where('Name', 'LIKE', "%$query%")->get();

Try using orWhere() as part of your query to add additional constraints.
$rawResults = DB::table('students')
->select('Name', 'Date', 'Class')
->where('Name', 'LIKE', "%{$query}%")
->orWhere('Class', 'LIKE', "%{$query}%")
->get();

Related

Laravel: Using multiple search terms to get an entry

Right now, I am grabbing users who fit a certain criteria like this:
$searchTerm = 'MULTI';
$multiUser = USER::where('code', 'LIKE', '%'.$searchTerm.'%')->get();
Now I changed the code to be "MANY" but we have a lot of users who already have MULTI as their code so I need the searchTerm to grab both. Can I do this in one query?
User::query()
->where('code', 'LIKE', '%MULTI%')
->orWhere('code', 'LIKE', '%MANY%')
->get();

Laravel where clause is not returning data after multiple orWhere clauses?

I am using laravel 6.10 version in that I am implementing search,
I have 3 tables
1) course_category
2) course_sub_category
3) course [course_id_category(foreign key),course_sub_category_id(foreign key)]
below is my code
$course = $request->searchItem;
if ($course=="") {
$Courses = DB::table('course')
->join('course_category', 'course.course_category_id', '=', 'course_category.id')
->select('course.*','course_category.title as category_title','course_category.thumb as category_thumb')
->orderBy('course.title','asc')
->paginate(15);
}
else{
$Courses = DB::table('course')
->join('course_category', 'course.course_category_id', '=', 'course_category.id')
->join('course_sub_category', 'course.course_sub_category_id', '=', 'course_sub_category.id')
->select('course.*','course_category.title as category_title','course_category.thumb as category_thumb')
->where('course.title', 'LIKE', '%'.$course.'%')
->orWhere('course_category.title', 'LIKE', '%'.$course.'%')
->orWhere('course_sub_category.title', 'LIKE', '%'.$course.'%')
->get();
}
when i am retunring the values i am gatting 0 arrays but when i am removing one orwhere from existing my query is working and its returnig we all values with match
means when i am using multiple orWhere in laravel my where is not working, please share the solution on same.
->orWhere doesnt work like typical SQL. You should use it like that:
$Courses = DB::table('course')
->join('course_category', 'course.course_category_id', '=', 'course_category.id')
->join('course_sub_category', 'course.course_sub_category_id', '=', 'course_sub_category.id')
->select('course.*','course_category.title as category_title','course_category.thumb as category_thumb')
->where(function($query) use ($course){
$query->where('course.title', 'LIKE', '%'.$course.'%')
$query->orWhere('course_category.title', 'LIKE', '%'.$course.'%')
$query->orWhere('course_sub_category.title', 'LIKE', '%'.$course.'%')
})
->get();

Multiple like in a laravel mongo db package not working

I have a project which is creating in laravel and mongodb. I am using a package for connecting to mongodb "https://github.com/jenssegers/laravel-mongodb". I have a table listing. In this table listing there is a text box for search data. For that I used like feature. I have followed the same in documentation but not working. When I search invoice_number data is getting empty. But when I search beds data is getting perfectly. Please correct me.
$bookings = Booking::select('invoice_number', 'temp_user_id', 'user', 'checkin_from', 'reserve_to', 'beds', 'dormitory', 'sleeps', 'status', 'payment_status', 'payment_type', 'total_prepayment_amount', 'txid')
->where('is_delete', 0)
->where('invoice_number', 'like', "%{$search}%")
->orWhere('beds', 'like', "%{$search}%")
->skip($start)
->take($limit)
->orderBy($order, $dir)
->get();
The WHERE statements in your query translate to this:
WHERE [cond_1] AND [cond_2] OR [cond_3]
I doubt this is the condition you need. I'd say you want this:
WHERE [cond_1] AND ([cond_2] OR [cond_3]) -- notice the brackets.
To achieve this, you need a closure in your Builder query. Try this:
$bookings = Booking::select('invoice_number', 'temp_user_id', 'user', 'checkin_from', 'reserve_to', 'beds', 'dormitory', 'sleeps', 'status', 'payment_status', 'payment_type', 'total_prepayment_amount', 'txid')
->where('is_delete', 0)
->where(function($query) use ($search) { /* That's the closure */
$query->where('invoice_number', 'like', "%{$search}%")
->orWhere('beds', 'like', "%{$search}%");
})
->skip($start)
->take($limit)
->orderBy($order, $dir)
->get();
And an example from the docs.

Laravel Eloquent Search Multiple Related Tables

I am struggling with figuring out how to run a like query against multiple related tables.
I have a submissions table that has related users, mcd_forms, and submission_statuses tables.
Here is my code for running a LIKE statement with the given $terms_like.
$submission = new Submission;
$terms_like = '%'.$search_terms.'%';
$data['submissions'] = $submission
->join('users as users', 'users.id', '=', 'submissions.user_id')
->join('mcd_forms as forms', 'forms.submission_id', '=', 'submissions.id')
->join('submission_statuses as statuses', 'statuses.id', '=', 'submissions.submission_status_id')
->where(function($q) use ($terms_like) {
$q->where('users.user_group_id', '=', Auth::user()->user_group_id)
->orWhere('forms.name', 'like', $terms_like)
->orWhere('forms.custom_id', 'like', $terms_like)
->orWhere('forms.start_date', 'like', $terms_like)
->orWhere('forms.end_date', 'like', $terms_like)
->orWhere('forms.soft_sell_date', 'like', $terms_like)
->orWhere('forms.region', 'like', $terms_like)
->orWhere('statuses.status_formatted', 'like', $terms_like);
});
No matter what I try it returns incorrect results. What am I doing wrong?
In your query above, since you are not using the "%" symbol, your like clause is working as an "=" since it's trying to match the whole word.
Replace all the "where" clause like this:
->orWhere('forms.name', 'like', "%".$terms_like."%")
This will try to match the word anywhere in the text.
You can try 'like' operator following this:
$users = DB::table('users')
->where('forms.name','LIKE', '%'.$terms_like.'%')
->get();

Query for multiple fields with Laravel

So I am trying to create a search functionality so that you can query for both first and last name. What can I use as an AND statement for the query?
public function find()
{
$search = Input::get('contact_search');
$query = Contact::orderBy('name', 'desc');
if (is_null($search))
{
$contacts = $query->paginate(15);
//return View::make('contacts.index')->with(array('contacts' => $contacts));
} else {
$contacts = $query->where('firstName', 'LIKE', "%{$search}%")
->orWhere('lastName', 'LIKE', "%{$search}%")
->paginate(15);
$contact = Contact::find(1);
}
return View::make('hello')->with(array('contacts' => $contacts));
}
I have tried
$query->where('firstName', 'LIKE', "%{$search}%")
->Where('lastName', 'LIKE', "%{$search}%")
but that does not work either. Any advice would be awesome! Thanks.
where() acts as an and statement already. Just chain them together.
$people = People::where('first_name','=','John')->where('last_name','=','Doe')->get();
In your query, you have ->Where. Make sure its lowercase.
Also, your method could use some optimization. You are searching for multiple contacts and paginating the results, but then you are doing a find(1) for some reason. A better approach is to just do the following:
$contact = Contact::orderBy('name','desc')
->where('firstName', 'LIKE', "%{$search}%")
->where('lastName', 'LIKE', "%{$search}%")
->first();
That will return your first contact in the results. No need for pagination. And find() actually searches for records based off of id's anyways so you don't want to use that in this case.
If you use where it will add AND if you use multiple where. However I don't know if in your case you want to use AND because you if someone will put into form Jo you will search for poeple that hat Jo both in name and surname, so for example John Smith won't be found here because his surname doesn't contain Jo.
So answering your question you could use:
$contacts = $query->where('firstName', 'LIKE', "%{$search}%")
->where('lastName', 'LIKE', "%{$search}%")
->paginate(15);
but probably this won't make much sense.
It's hard to also say what exactly you do here, because you have here:
$contacts = $query->where('firstName', 'LIKE', "%{$search}%")
->orWhere('lastName', 'LIKE', "%{$search}%")
->paginate(15);
$contact = Contact::find(1);
using those 2 lines first you look for people who have $search in first or last name and paginate them, and when using Contact::find(1); you find person with id 1. It also doesn't seem to be good solution to anything here.
If you would like to find the first record that have $search either in first or last name, you should use:
$contacts = $query->where('firstName', 'LIKE', "%{$search}%")
->where('lastName', 'LIKE', "%{$search}%")
->first();
without orWhere and without paginate.

Categories