I'm building an application on Laravel where I need to join the same table with some condition
I'm having a project_associate_brand table which includes following columns:
project_id | brand_id | product_group_id | product_sub_group_id
I need to fetch out brands associated with project with respect to product_group_id and product_sub_group_id and also which are the other brands associated with them
To execute this I tried:
AssociatedBrands::where('project_associate_brand.brand_id', '=', $id)
->leftjoin('project_associate_brand as pr_brand', function ($join) {
$join->on('project_associate_brand.project_id', '=', 'pr_brand.project_id')
->where('project_associate_brand.product_group_id', '=', 'pr_brand.product_group_id')
->where('project_associate_brand.product_subgroup_id', '=', 'pr_brand.product_subgroup_id');
})
->leftjoin('brands as b', function ($join) {
$join->on('pr_brand.brand_id', '=', 'b.id')->whereNotIn('pr_brand.brand_id', [$id]);
})
->select('project_associate_brand.*', 'b.id as other_brand_id', 'b.title as other_brand_title')
->paginate(15);
I'm not getting the desired results, I tried removing few of the where clause, when I remove product_group_id and product_sub_group_id contraints I was able to get results
AssociatedBrands::where('project_associate_brand.brand_id', '=', $id)
->leftjoin('project_associate_brand as pr_brand', function ($join) {
$join->on('project_associate_brand.project_id', '=', 'pr_brand.project_id');
//Removed other where clause
})
->leftjoin('brands as b', function ($join) {
$join->on('pr_brand.brand_id', '=', 'b.id')->whereNotIn('pr_brand.brand_id', [$id]);
})
->select('project_associate_brand.*', 'b.id as other_brand_id', 'b.title as other_brand_title')
->paginate(15);
Any better approach is welcome. Further I would like to execute with() eager loading of relations
Related
I have a collection of user skills but i need is to get only skills as an array i tried this
$builder->where('job_id', $value)
->join('users', 'job_applicants.user_id', '=', 'users.id')
->join('user_skills', 'users.id', '=', 'user_skills.user_id', function ($join){
$join->selectRaw("GROUP_CONCAT(user_skills.skill, ', ')");
})
->groupBy('job_applicants.job_id')
->get();
The exception message : Object of class Closure could not be converted to string
The join closure should be the first and only argument after the table name.
->join('user_skills', function ($join) {
$join->on('users.id', '=', 'user_skills.user_id')
->selectRaw("GROUP_CONCAT(user_skills.skill, ', ')");
});
However, a selectRaw inside the join closure doesn't make a whole lot of sense. Logically, this isn't where you select data. You've joined the table, your select statement belongs in the main query, it doesn't belong contained within the join, like so:
$builder->selectRaw("job_applicants.*, GROUP_CONCAT(user_skills.skill, ', ')")
->where('job_id', $value)
->join('users', 'job_applicants.user_id', '=', 'users.id')
->join('user_skills', 'users.id', '=', 'user_skills.user_id')
->groupBy('job_applicants.job_id')
->get();
Good day all, I am trying to count all records in a table but only if the table does not contain data in a specific column (deleted_at). It is a join table the table names are companies and employees. I am currently counting the records with a DB::raw but it should only count it if the deleted_at column is null. Please understand that i am a beginner.
public function index()
{
$user = Auth::user()->id;
$companies = DB::table('companies AS c')
->select([
'c.id',
'c.logo',
'c.company_name',
'c.created_at',
'c.sector',
'c.deleted_at',
DB::raw('COUNT(e.id) AS employee_count')
])
->leftJoin('employees AS e', 'e.company_id', '=', 'c.id' )
->leftJoin('company_user AS cu', 'cu.company_id', '=', 'c.id')
->where('cu.user_id', '=', $user)
->where('c.deleted_at', '=', null)
->groupBy('c.id')
->get();
return view('account.companies.index')
->with('companies', $companies);
}
If you are using Mysql then you could use conditional aggregation
$companies = DB::table('companies AS c')
->select([
'c.id',
'c.logo',
'c.company_name',
'c.created_at',
'c.sector',
'c.deleted_at',
DB::raw('SUM(c.deleted_at IS NULL) AS employee_count')
])
->leftJoin('employees AS e', 'e.company_id', '=', 'c.id' )
->leftJoin('company_user AS cu', 'cu.company_id', '=', 'c.id')
->where('cu.user_id', '=', $user)
->groupBy('c.id')
->get();
In mysql when an expression is used inside sum(a= b) it will result as a boolean 0/1 so you can get your conditional count using above
Or you could use whereNull() method in your query
->whereNull('c.deleted_at')
Use this code:
$employeeCount = DB::table('employees')
->select('companies.name', DB::raw('count(employees.id) as employee_count'))
->join('companies', 'employees.company','=','companies.id')
->groupBy('companies.id')
->get();
I need to join two tables in laravel model. Here is my query,
$id="1002";
$resultsInCalls = DB::table('qlog')
->select('user_master.fname','user_master.lname','qlog.data2')
->whereDate('created', '=', date('Y-m-d')) // this day
->join('qlog', 'qlog.agent', '=', 'user_master.sip_id')
->where('agent', '=', $id)
->where(function ($query) {
$query->where('event', '=', 'COMPLETEAGENT')
->orWhere('event', '=', 'COMPLETECALLER');
})
->take(5)
->get();
But I run this query I get following error.
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'qlog' (SQL: select `user_master`.`fname`, `user_master`.`lname`, `qlog`.`data2` from `qlog` inner join `qlog` on `qlog`.`agent` = `user_master`.`sip_id` where date(`created`) = 2017-11-29 and `agent` = 1002 and (`event` = COMPLETEAGENT or `event` = COMPLETECALLER) limit 5)
I read related questions. But I can't figure it out. How to fix this error.
Here I get it done,
$resultsInCalls = DB::table('qlog')
->join('user_master as user_table', 'qlog.agent', '=', 'user_table.sip_id')
->select('user_table.fname','user_table.lname','qlog.data2')
->whereDate('created', '=', date('Y-m-d')) // this day
->where('agent', '=', $id)
->where(function ($query) {
$query->where('event', '=', 'COMPLETEAGENT')
->orWhere('event', '=', 'COMPLETECALLER');
})
->take(5)
->get();
I have 2 tables 'users' and 'instantUsers'. I want to join them on users.id = instantUsers.user_id and want to add 2 where clauses on the resulting. I'm not getting how to do both. The query I'm using is -
DB::table('users')
->join('instantUsers', function($join) use ($userId) {
$join->on('users.id', '=', 'instantUsers.user_id');
})
->where('instantUsers.instantMode', '=', '1')
->where (function($query) use ($userId) {
$query->where('instantUsers.user_id', '!=', $userId);
})
->get();
You can try this one maybe this will help you:
DB::table('users as table1')->join('instantUsers as table2','table1.id','=','table2.fkId') ->where('table2.instantMode','=','1')->where('table2.user_id','!=',$userId)->get();
Your 'instantUsers.instantMode','=','1' expression can be done in a join, resulting in a better performance.
I would write it like this
DB::table('users')
->join('instantUsers', function($join) use ($userId) {
$join
->on('users.id', '=', 'instantUsers.user_id')
->on('instantUsers.instantMode', '=', 1);
})
->where('users.id', '!=', $userId)
->get();
i have the sql below:
$result = DB::table('tblA')
->join('tblB','tblB.id', '=', 'tblA.photoid')
->join('tblB','tblB.id', '=', 'tblA.linkedphotoid')
...
I have tblA with photoid and linkedphotoid and both link to one same table(tblB) and same column.
How to write it in laravel sql?
You can add OR condition in your join
$result = DB::table('tblA')
->join('tblB', function($join) {
$join->on('tblB.id', '=', 'tblA.photoid')->orOn('tblB.id', '=', 'tblA.linkedphotoid');
})
->get();