Laravel eloquent double join returning empty - mysql

I have a laravel eloquent db query that I would like to return results based on chained join.
$replies = DB::table('model_replies')
->join('support_tickets', function($join){
$join->on('model_replies.model_id', '=', 'support_tickets.id');
$join->where('model_replies.model', '=', DB::raw('"SupportTicket"'));
})
->join('produce_requests', function($join){
$join->on('model_replies.model_id', '=', 'produce_requests.id');
$join->where('model_replies.model', '=', DB::raw('"ProduceRequest"'));
})
->get();
I followed the example from the docs here.
The problem is when I chain the join statements as suggested here my query gives no results.
A single join works just fine. But I need to be able to chain several of them.
What could I be doing wrong?
Thank you!

If I understood it correctly. You want to get support_tickets for those model_replies where model_replies.model = SupportTicket
DB::table('model_replies')
->leftJoin('support_tickets', function($join){
$join->on('model_replies.model_id', '=', 'support_tickets.id');
$join->on('model_replies.model', '=', DB::raw('"SupportTicket"'));
})
->leftJoin('produce_requests', function($join){
$join->on('model_replies.model_id', '=', 'produce_requests.id');
$join->where('model_replies.model', '=', DB::raw('"ProduceRequest"'));
})
->get();

Related

Laravel Query Build with nested Right Joins

I have a query that works but I need to get it into the Query Builder:
RIGHT JOIN (tlkpSUDesc RIGHT JOIN tblSU ON tlkpSUDesc.SUDescCode = tblSU.SUDescCode) ON tlkpSUTyp.SUTypCode = tblSU.SUTypCode
I can figure out how to do it. I tired this:
->rightJoin('tlkpSUDesc', function($join) {
$join->on('tblSU', 'tlkpSUDesc.SUDescCode', '=', 'tblSU.SUDescCode');
}
'tlkpSUTyp.SUTypCode', '=', 'tblSU.SUTypCode')
But that didn't work. I also tried
->rightJoin('tlkpSUDesc', 'tlkpSUTyp.SUTypCode', '=', 'tblSU.SUTypCode')
->rightJoin('tblSU', 'tlkpSUDesc.SUDescCode', '=', 'tblSU.SUDescCode')
But that didn't work either.
Thanks!
I agree with #matiaslauriti that there's nothing wrong with reaching for raw expressions. But, since you asked and it can be done with the query builder, try this:
$query = \DB::query();
$query->rightJoin('tlkpSUDesc', function ($query) {
$query->rightJoin('tblSu', 'tlkpSUDesc.SUDescCode', '=', 'tblSU.SUDescCode')->on(
'tlkpSUTyp.SUTypCode', '=', 'tblSU.SUTypCode');
})->toSql();

How to make leftjoin in laravel 5.4

I have two table 1)clients 2)orders. I trying to make leftjoin but I not getting correct result what i want. Please see image below.
Thanks
ClientController.php
$clients = Client::leftjoin('orders','orders.client_id','=','clients.id')
->select(DB::raw("count(orders.id) as orderCount"),
'clients.name as name','clients.email as email',
'clients.phone as phone','clients.country as country',
'orders.total_amount')
->where('orders.status',0)
->get();
$myevents = DB::table('users')
->join('event_model_user', 'users.id', '=', 'event_model_user.user_id')
->rightJoin('events', 'event_model_user.event_model_id', '=', 'events.id')
->where('users.id','=',$user->id)
->orWhere('events.bookable','=',$user->zipcode)
->get();
this is an example of i used a right join to get the events where no user has done any booking!
in your case this would be something like (not tested)
$myevents = DB::table('clients')
->leftJoin('orders','orders.client_id','=','clients.id')
->where('orders.status',0)
->get();

MYSQL QUERY multiple tables count

I am having problems building a query which delimit the number of lawyers by branch of law in one particular territory(province) of the country.
I have four tables, one with the branchlaws, one for users, one where lawyers add branches where the practice law and one for territories.
I am able to summarize all this information with the following query:
$lawyersbyprovince = DB::table('branchlawsubareas')
->leftJoin('lawyerbranches', function($join) {
$join->on( 'branchlawsubareas.id', '=', 'lawyerbranches.subarea_id');
})
->leftJoin('users', function($join) {
$join->on( 'lawyerbranches.user_id', '=', 'users.id');
})
->leftJoin('states', function($join) {
$join->on('users.working_province', '=', 'states.id_state');
})
->leftJoin('branchlaws', 'branchlawsubareas.area_id', '=', 'branchlaws.id')
->select('branchlawsubareas.name as subarea',
DB::raw('count(lawyerbranches.subarea_id) as total'),
'branchlawsubareas.id'
)
->where('states.id_state','=', $province)
->groupBy('branchlawsubareas.id')
->get();
This query returns the number of lawyers per branch of law but it does not delimit by province. I have spent enough time trying things but obviously I missing something.
Any help will be much appreciated.
Try not to join instead the states of leftjoin.
$lawyersbyprovince = DB::table('branchlawsubareas')
->leftJoin('lawyerbranches', 'branchlawsubareas.id', '=', 'lawyerbranches.subarea_id')
->leftJoin('users', 'lawyerbranches.user_id', '=', 'users.id')
->leftJoin('branchlaws', 'branchlawsubareas.area_id', '=', 'branchlaws.id')
->join('states', 'users.working_province', '=', 'states.id_state')
->where('states.id_state','=', $province)
->select('branchlawsubareas.name as subarea',
DB::raw('count(lawyerbranches.subarea_id) as total'),
'branchlawsubareas.id'
)
->groupBy('branchlawsubareas.id')
->get();
Now if this does not work, you have another way. Remove where() clause and change your select like this
...
->select(''branchlawsubareas.id', 'states.id as state_id')
...
This will return a collection with ALL the data. Then you can manipulate that collection like this
$newCollection = $resultFromQuery->where('state_id', $province);
$newCollection->count(); //number of laywer in that state
I hope this helps, or simply gives you a hint.

InvalidArgumentException in JoinClause.php line 79: Not enough arguments for the on clause

I did query builder with laravel and I've used multiple join in it. When execute the query I get the error Not enough arguments for the on clause.
My query builder:
return DB::table('towns_cat_rel')
->join('towns', function ($join) {
$join->on('towns_cat_rel.town_id', '=', 'towns.id')
->where('publish', '=', 1);
})
->join('towns_translations', function ($join) {
$join->on('towns_cat_rel.town_id', '=', 'towns_translations.town_id')
->where('locale', '=', \App::getLocale());
})
->join('towns_cat', function ($join) use($categoryID) {
$join->on('towns_cat.id', '=', 'towns_cat_rel.town_cat_id')
->where('towns_cat.id', '=', $categoryID);
})
->join('towns_sub_cat_rel', 'towns_cat_rel.town_id', '=', 'towns_sub_cat_rel.town_id')
->join('towns_sub_cat', function ($join) use($subCategoryID) {
$join->on('towns_sub_cat_rel.town_sub_cat_id', 'towns_sub_cat.id')
->where('towns_sub_cat.id', '=', $subCategoryID);
})
->get();
Does anyone know why this happen?
Sorry Guys! I had forget '=' in the last join => $join->on('towns_sub_cat_rel.town_sub_cat_id', '=', 'towns_sub_cat.id')
Now it works fine!
I am using laravel5.2 and facing this above error message due to this above code and i resolved with this code, you can modify as per your requirement.
DB::table('users')
->join('contacts', function ($join) {
$join->on('users.id', '=', 'contacts.user_id')->orOn(...);
})
->get();

laravel advanced join on 5 tables, with 2 tables campared

I'm not even sure this is possible, I can get it to work up to the 2nd join function, then it just return null.
$articles = DB::table('courses')
->join('version','courses.current_version_id','=','version.id')
->join('category as c1', function($join1){
$join1->on('courses.code','=','c1.title');
})
->join('category as c2', function($join2){
$join2->on('version.version_number','=','c2.title');
})
->join('articleCategories','c2.id','=','articleCategories.cat_id')
->join('articles','articleCategories.article_id','=','articles.id')
->where('c2.parent_id','=','c1.id')
->where('courses.id','=',$course)
->select('courses.code','courses.title','version.version_number','articles.*')
->get();
Is this even possible? What am I getting wrong?
You may try something like this:
$fieldsToSelect = array(
'courses.code',
'courses.title',
'courses.current_version_id',
'version.version_number',
'version.id',
'category.id',
'category.title',
'articleCategories.cat_id',
'articleCategories.article_id',
'articles.*'
);
$articles = DB::table('courses')->join('version', function($join){
$join->on('courses.current_version_id', '=', 'version.id');
})->join('category', function($join) {
$join->on('courses.code', '=', 'category.title')
->on('version.version_number', '=', 'category.title');
})->join('articleCategories', function($join){
$join->on('category.id', '=', 'articleCategories.cat_id');
})->join('articles', function($join){
$join->on('articleCategories.article_id', '=', 'articles.id');
})
->get($fieldsToSelect);