where clause in laravel for search - php

I am trying to select rows from table students using laravel query for searching but on searching it's returning all rows from table.
$data = DB::table('students')
->select(['name', 'username'])->where('institute', $inst)
->where('username', 'LIKE', '%'.$search.'%')
->orWhere('name', 'LIKE', '%'.$search.'%')
->orWhere('email', 'LIKE', '%'.$search.'%')
->orWhere('contact', 'LIKE', '%'.$search.'%')
->orderBy('id', 'DESC')
->paginate(10);

This is the thing, at least for what I can see, you have two kind of filters:
Students that are in a given institute
Students that have a similar username, name, email or contact than the given search term
If so, the problem in your query are the orWhere() clauses. Let's simplify your query to illustrate the issue:
$data = DB::table('students')
->where('institute', $inst)
->orWhere('name', 'LIKE', '%'.$search.'%')
->get();
In the above code, the statement will get all the students that have the institute equals to $inst OR have a name similar to %search%. So, the results could include students that are not in the given institute. You see the issue now?
In order to first filter by institute AND get records that have a similar similar username, name, email or contact, you could wrap the second conditions in a closure. Try this:
$data = DB::table('students')
->select(['name', 'username'])
->where('institute', $inst)
->where(function ($query) use ($search) { // <<<
$query->where('username', 'LIKE', '%'.$search.'%')
->orWhere('name', 'LIKE', '%'.$search.'%')
->orWhere('email', 'LIKE', '%'.$search.'%')
->orWhere('contact', 'LIKE', '%'.$search.'%');
})
->orderBy('id', 'DESC')
->paginate(10);

Related

Laravel 8 Livewire search filter not working with first where clause

I want to have a table which shows all the data per company that has a filter search input, the problem is when I put the first argument where('company_id', Auth::user()->company_id) it bugs out and doesn't make the filter search work.
$groups = Group::where('company_id', Auth::user()->company_id)
->orWhere('code', 'like', $filter)
->orWhere('name', 'like', $filter)
->orWhere('description', 'like', $filter)
->orWhereRaw("(CASE WHEN active = 1 THEN 'Active' ELSE 'Inactive' END) LIKE '%$filter%'")
->orderBy($this->column, $this->order)
->paginate($this->size);
This is my query for the table.
I suppose you want company_id filter to apply to all query and combine other orWhere conditions. The other orWhere should be grouped in query like this:
$groups = Group::where('company_id', Auth::user()->company_id)
->where(function ($query) use ($filter) {
return $query->where('code', 'like', $filter)
->orWhere('name', 'like', $filter)
->orWhere('description', 'like', $filter)
->orWhereRaw("(CASE WHEN active = 1 THEN 'Active' ELSE 'Inactive' END) LIKE '%$filter%'");
})
->orderBy($this->column, $this->order)
->paginate($this->size);
I have solved the query, by adding another where statement with the other orWhere the syntax is below.
For some reason the syntax above worked for Vue but when I migrated Vue to Livewire
$groups = Group::where('company_id', Auth::user()->company_id)
->where('code', 'like', $filter)
->orWhere('name', 'like', $filter)
->orWhere('description', 'like', $filter)
->orWhereRaw("(CASE WHEN active = 1 THEN 'Active' ELSE 'Inactive' END) LIKE '%$filter%'")
->orderBy($this->column, $this->order)
->paginate($this->size);

Laravel SQL Query whereIn + where + orWhere

I'm having some trouble with this query and I'm out of ideas, let me explain:
Here is what I have:
Table of users (Columns that matter for this question: name, description)
Table of services (Columns that matter for this question: name, description, subcategory_id, category_id)
Pivot table for the user-services relationship
Here is what I want to happen:
The app must be able to search by text in the name/description of the user and his related services.
Also the app can send me one category_id and multiple subcategory_ids for the search, so the related services must have that id
First search works alright, problem comes when I try the second one. Is like it doesn't take into account every parameter of the query. I want it to search by text ONLY if that category_id or subcategory_id matches, but it shows results without having that match.
Here is what I've got so far:
$professionals = \DB::table('users')->where('role_id', Role::PROFESSIONAL_USER)
->join('professional_service', 'users.id', '=', 'professional_service.user_id', 'left')
->join('services', 'services.id', '=', 'professional_service.service_id', 'left')
->select('users.id', 'users.name as name', 'users.last_name as last_name', 'users.gender as gender', 'users.description as description', 'users.city', 'users.latitude', 'users.longitude', 'users.wpicture')
->where('users.name', 'like', '%'.$request->search.'%')
->where('services.category_id', '=', $request->category_id)
->orWhere('users.description', 'like', '%'.$request->search.'%')
->orWhere('services.name', 'like', '%'.$request->search.'%')
->orWhere('services.description', 'like', '%'.$request->search.'%')
->whereIn('services.subcategory_id', $request->subcategories)
->groupBy('users.id')
->paginate($request->per_page);
Any help would be appreciated. Thanks!
Edited (using closure as suggested in comments):
$professionals = \DB::table('users')->where('role_id', Role::PROFESSIONAL_USER)
->join('professional_service', 'users.id', '=', 'professional_service.user_id', 'left')
->join('services', 'services.id', '=', 'professional_service.service_id', 'left')
->select('users.id', 'users.name as name', 'users.last_name as last_name', 'users.gender as gender', 'users.description as description', 'users.city', 'users.latitude', 'users.longitude')
->where(function($query) use ($search){ $query->where('users.name', 'like', '%'.$search.'%'); $query->orWhere('users.description', 'like', '%'.$search.'%'); $query->orWhere('services.name', 'like', '%'.$search.'%'); $query->orWhere('services.description', 'like', '%'.$search.'%');})
->where('services.category_id', '=', $request->category)
->whereIn('services.subcategory_id', explode(',', $request->subcategories))
->groupBy('users.id')
->paginate($request->per_page);
Problem now is, if the user only search by text and category / subcategories are null, no results are shown. How can I ignore them if they are null?
Found the solution (I'm sure it can be done in a better way, but this is working for me):
$professionals = \DB::table('users')->where('role_id', Role::PROFESSIONAL_USER)
->join('professional_service', 'users.id', '=', 'professional_service.user_id', 'left')
->join('services', 'services.id', '=', 'professional_service.service_id', 'left')
->select('users.id', 'users.name as name', 'users.last_name as last_name', 'users.gender as gender', 'users.description as description', 'users.city', 'users.latitude', 'users.longitude')
->where(function($query) use ($search){ $query->where('users.name', 'like', '%'.$search.'%'); $query->orWhere('users.description', 'like', '%'.$search.'%'); $query->orWhere('services.name', 'like', '%'.$search.'%'); $query->orWhere('services.description', 'like', '%'.$search.'%');})
->when($category, function($query) use ($category) { $query->where('services.category_id', '=', $category);})
->when($subcategories, function($query) use ($subcategories) { $query->whereIn('services.subcategory_id', $subcategories);})
->groupBy('users.id')
->paginate($request->per_page);
Thanks for the tip about closures.

How to write this query in laravel? Any Suggestions?

This is the Query which is working fine in MY SQL:
SELECT users.name FROM ((event_user INNER JOIN events ON
event_user.event_id = events.id) INNER JOIN users ON
event_user.user_id = users.id) where events.name like '%FEB%' or
users.name like '%FEB%' or users.email like '%FEB%' or users.phone
like '%FEB%'
This is what I've written which is of course not working:
$data['search_data'] = EventUser::select('users.*')
->join
('users', 'event_user.user_id', '=', 'users.id')
->join
('events', 'event_user.event_id', '=', 'events.id')
->where
('events.name','like', "'$search'")
->orWhere
("users.id",'like', "'$search'")
->orWhere
("users.name",'like', "'$search'")
->orWhere
("users.phone",'like', "'$search'")
->orWhere
("users.email",'like', "'$search'")
->orWhere
("users.password",'like', "'$search'")
->orWhere
("users.work_location",'like', "'$search'")
->orWhere
("users.profession",'like', "'$search'")
->orWhere
("users.designation",'like', "'$search'")
->orWhere
("users.committee",'like', "'$search'")
->orWhere
("users.role",'like', "'$search'")
->orWhere
("users.bio",'like', "'$search'")
->paginate(10);
ok so, with the help from everyone.
I've got solution. Thank you all.
$data['search_data'] = EventUser::select('users.*', 'events.name as e_name')
->join('users', 'event_user.user_id', '=', 'users.id')
->join('events', 'event_user.event_id', '=', 'events.id')
->where('events.name', 'like', '%'.$search.'%')
->orWhere('users.id', 'like', '%'.$search.'%')
->orWhere('users.name', 'like', '%'.$search.'%')
->orWhere('users.phone', 'like', '%'.$search.'%')
->orWhere('users.email', 'like', '%'.$search.'%')
->orWhere('users.password', 'like', '%'.$search.'%')
->orWhere('users.work_location', 'like', '%'.$search.'%')
->orWhere('users.profession', 'like', '%'.$search.'%')
->orWhere('users.designation', 'like', '%'.$search.'%')
->orWhere('users.committee', 'like', '%'.$search.'%')
->orWhere('users.role', 'like', '%'.$search.'%')
->orWhere('users.bio', 'like', '%'.$search.'%')
->distinct()
->paginate(10);
But this is still returning the repetitive results.
You need to concat $search with % like this:
->where('events.name','like', '%'.$search.'%')
hmm, thats a long query, I think you can test this:
$data['search_data'] = EventUser::select('users.*')
->join('users', 'event_user.user_id', '=', 'users.id')
->join('events', 'event_user.event_id', '=', 'events.id')
->where('events.name', 'like', '%'.$search.'%')
->where(function ($query) use ($search) {
$query->where('title', 'LIKE', '%'.$search.'%')
->orWhere('users.id', 'LIKE', '%'.$search.'%')
->orWhere('users.name', 'LIKE', '%'.$search.'%')
->orWhere('users.phone', 'LIKE', '%'.$search.'%')
->orWhere('users.email', 'LIKE', '%'.$search.'%')
->orWhere('users.password', 'LIKE', '%'.$search.'%')
->orWhere('users.work_location', 'LIKE', '%'.$search.'%')
->orWhere('users.profession', 'LIKE', '%'.$search.'%')
->orWhere('users.designation', 'LIKE', '%'.$search.'%')
->orWhere('users.committee', 'LIKE', '%'.$search.'%')
->orWhere('users.role', 'LIKE', '%'.$search.'%')
->orWhere('users.bio', 'LIKE', '%'.$search.'%')
})
->paginate(10);
Hope it help. Let us know if it worked!

laravel eloquent query orWhere

In Normal PHP i have a sql query
$q = 'shojib'; or $q = '01913711...'; or $q = 'House 23...';
select * from member where LoginName like '%$q%' or mobile like '%$q%' or ConnectionAdderss like '%$q%' order by LoginName LIMIT 10;
i want to write this query in laravel.
Please help me.
i am using SoftDeletingTrait in laravel Member Model
Member::where('zone', 'like', '%'.$key.'%')
->orWhere('login_name', 'like', '%'.$key.'%')
->orWhere('mobile', 'like', '%'.$key.'%')
->orWhere('name', 'like', '%'.$key.'%')
->orderBy('login_name')
->take('100')
->get();
But it will return all data from database with deleted_at is not null;
it should return only those data whose deleted_at is null;
You should write the query as below
Member::onlyTrashed()->where('zone', 'like', '%'.$key.'%')
->orWhere('login_name', 'like', '%'.$key.'%')
->orWhere('mobile', 'like', '%'.$key.'%')
->orWhere('name', 'like', '%'.$key.'%')
->orderBy('login_name')
->take('100')
->get();
After certain times i have found solution
It should be
$members = Member::where(function($query) use ($key)
{
$query->where('zone', 'like', '%'.$key.'%')
->orWhere('login_name', 'like', '%'.$key.'%')
->orWhere('contact_number', 'like', '%'.$key.'%')
->orWhere('sms_comn', 'like', '%'.$key.'%')
->orWhere('email', 'like', '%'.$key.'%')
->orWhere('name', 'like', '%'.$key.'%');
})
->take('100')
->orderBy('login_name', 'ASC')
->get();

Left join not working properly

I'm trying to search all the users registered for a particular course using the following query using the laravel.
$users = DB::table('users')
->leftJoin('registrationrequests', 'users.id', '=', 'registrationrequests.user_id')
->where('firstname', 'LIKE', "%$search%")
->orWhere('lastname', 'LIKE', "%$search%")
->orWhere('username', 'LIKE', "%$search%")
->where('registrationrequests.course_id', $course_id)
->where('registrationrequests.registered', 1)
->get();
In the table registrationrequests, I have columns for course_id, user_id and registered (binary value). course_id and user_id are foreign keys for respective tables.
I'm getting an array of output. But it's not checking the condition where('registrationrequests.course_id', $course_id)
What might be the reason?
Using http://laravel.com/docs/4.2/queries#advanced-wheres and https://stackoverflow.com/a/18660266/689579 your query would look something like -
$users = DB::table('users')
->leftJoin('registrationrequests', 'users.id', '=', 'registrationrequests.user_id')
->where('registrationrequests.course_id', $course_id)
->where('registrationrequests.registered', 1)
->where(function($users) use($search){
$users->where('firstname', 'LIKE', "%$search%")
->orWhere('lastname', 'LIKE', "%$search%")
->orWhere('username', 'LIKE', "%$search%")
})
->get();

Categories