Left join not working properly - php

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();

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.

where clause in laravel for search

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);

Laravel where clause is not working after multiple orWhere clauses?

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);

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."%");

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();

Categories