Laravel where clause is not working after multiple orWhere clauses? - php

I am querying vehicles with model name and type but these where clauses are not at the end after addingorWhere. When I comment or remove these orWhere Clauses then its work.
$models = Vehicle::join('vmodels', 'vmodels.vehicle_id', '=', 'vehicles.id')
->join('vehicletypes', 'vehicletypes.id', '=', 'vehicles.vehicletype_id')
->join('brands', 'brands.id', '=', 'vehicles.make')
->join('companies', 'brands.id', '=', 'companies.name')
->select('vehicles.slug as vslug', 'brands.name as brandname', 'vehicles.id as vid', 'vmodels.*', 'vehicletypes.name as vtype', 'companies.status as cstatus')
->where('brands.name', 'LIKE', "%{$term}%")
->orWhere('vmodels.name', 'LIKE', "%{$term}%")
->orWhere('vmodels.year', 'LIKE', "%{$term}%")
->orWhere('vehicletypes.name', 'LIKE', "%{$term}%")
->orWhere('vehicles.model', 'LIKE', "%{$term}%")
->where('companies.status', '=', 1 )
->where('vmodels.status', '=', 1)
->where('vehicles.status', '=', 1)
->paginate(30);

Group your orWhere's like,
$models = Vehicle::join('vmodels', 'vmodels.vehicle_id', '=', 'vehicles.id')
->join('vehicletypes', 'vehicletypes.id', '=', 'vehicles.vehicletype_id')
->join('brands', 'brands.id', '=', 'vehicles.make')
->join('companies', 'brands.id', '=', 'companies.name')
->select('vehicles.slug as vslug', 'brands.name as brandname', 'vehicles.id as vid', 'vmodels.*', 'vehicletypes.name as vtype', 'companies.status as cstatus')
->where(function($query) use($term){
$query->where('brands.name', 'LIKE', "%{$term}%")
->orWhere('vmodels.name', 'LIKE', "%{$term}%")
->orWhere('vmodels.year', 'LIKE', "%{$term}%")
->orWhere('vehicletypes.name', 'LIKE', "%{$term}%")
->orWhere('vehicles.model', 'LIKE', "%{$term}%");
})
->where('companies.status', '=', 1 )
->where('vmodels.status', '=', 1)
->where('vehicles.status', '=', 1)
->paginate(30);
Reference : https://laravel.com/docs/5.7/queries#parameter-grouping

if you want to simulate AND/OR statement for your SQL queries with Laravel query builder, you should do it like this:
((A or B) and (C or D)) or (E)
ModelName::join(...)->join(...)
->where(function($query){
$query->where( A )
->orWhere( B )
})
->where(function($query){
$query->where( C )
->orWhere( D )
})->orWhere( E )

How about this one:
$models = Vehicle::join('vmodels', 'vmodels.vehicle_id', '=', 'vehicles.id')
->join('vehicletypes', 'vehicletypes.id', '=', 'vehicles.vehicletype_id')
->join('brands', 'brands.id', '=', 'vehicles.make')
->join('companies', 'brands.id', '=', 'companies.name')
->select('vehicles.slug as vslug', 'brands.name as brandname', 'vehicles.id as vid', 'vmodels.*', 'vehicletypes.name as vtype', 'companies.status as cstatus')
->where('brands.name', 'LIKE', "%{$term}%")
->where('companies.status', '=', 1 )
->where('vmodels.status', '=', 1)
->where('vehicles.status', '=', 1)
->orWhere(function($query) use ($term){
$query->where('vmodels.name', 'LIKE', "%{$term}%")
->where('vmodels.year', 'LIKE', "%{$term}%")
->where('vehicletypes.name', 'LIKE', "%{$term}%")
->where('vehicles.model', 'LIKE', "%{$term}%")
})
->paginate(30);

Related

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 DB:table for multi where AND Or condition for multiple slected field

My query like :
$results = User::
where('this', '=', 1)
->where('that', '=', 1)
->where('this_too', '=', 1)
->where('that_too', '=', 1)
->where('this_as_well', '=', 1)
->where('that_as_well', '=', 1)
->where('this_one_too', '=', 1)
->where('that_one_too', '=', 1)
->where('this_one_as_well', '=', 1)
->where('that_one_as_well', '=', 1)
->get();
Solution for the multi where condition in laravel ,because its not working currently
->where('twd.status','=','0')->where(function($q) use($data){
$q->where('A','like', '%' .$data. '%')
->orWhere('B','like', '%' .$data. '%');
->orWhere('C','like', '%' .$search_param. '%');
})
You can do it like that .
Concat all column that you want to search
->where(DB::raw("concat(A,' ',B)"), 'like', "%".$data."%");

syntax to join a table twice in laravel

I'm trying to join the below 3 tables. The issue is in the part where I want to join posts.id to the shares.post_id using 'AND' operator which is not correct. What is the correct syntax to do that ?
$sharedPosts = DB::table('shares')
->join('users', 'users.id', '=', 'shares.user_id')
->join('posts', 'posts.user_id', '=', 'users.id' , 'AND' , 'posts.id', '=', 'shares.post_id')
->where('shares.user_id', '=', $myFriends)
->get();
Try aliasing:
$sharedPosts = DB::table('shares')
->join('users', 'users.id', '=', 'shares.user_id')
->join('posts as p1', 'p1.user_id', '=', 'users.id' , 'AND' , 'p1.id', '=', 'shares.post_id')
->join('posts as p2', 'p2.id', '=', 'shares.post_id')
->where('shares.user_id', '=', $myFriends)
->get();
You cannot join a table twice with no alias.
Although if you have 2 clauses in your join, I think you ought to do as in the documentation :
$sharedPosts = DB::table('shares')
->join('users', 'users.id', '=', 'shares.user_id')
->join('posts as p1', , function($join){
$join->on('p1.user_id', '=', 'users.id')
->on('p1.id', '=', 'shares.post_id')
})
->join('posts as p2', 'p2.id', '=', 'shares.post_id')
->where('shares.user_id', '=', $myFriends)
->get();
Edit (with one posts join):
$sharedPosts = DB::table('shares')
->join('users', 'users.id', '=', 'shares.user_id')
->join('posts', , function($join){
$join->on('posts.user_id', '=', 'users.id')
->on('posts.id', '=', 'shares.post_id')
})
->where('shares.user_id', '=', $myFriends)
->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