I am trying to pick out Shops with the specified $item_id.
An Item can have multiple Images, but I only want the first one.
The current code gives me only the first image, but it duplicates the item as many times as there are images.
Here's the code:
$shops = Shop::with(array('items' => function($query) use ($item_id) {
$query->select('items.id', 'items.name', 'items_images.path AS image');
$query->join('items_images', 'items.id', '=', 'items_images.item_id');
$query->where('items.id', '=', $item_id);
}))
->get(array('shops.id', 'shops.shop_name', 'shops.lat', 'shops.lng'));
And the current output:
How can I avoid getting all the duplicates?
I think this will work.. I haven't done much at all with this syntax but I think this is how it would work (assuming main is in the items_images table)
$shops = Shop::with(array('items' => function($query) use ($item_id)
{
$query->select('items.id', 'items.name', 'items_images.path AS image');
$query->join('items_images', function($join)
{
$join->on('items.id', '=', 'items_images.item_id')
->where('items_images.main', '=', 1);
})
$query->where('items.id', '=', $item_id);
}))->get(array('shops.id', 'shops.shop_name', 'shops.lat', 'shops.lng'));
Found the DOCS on it... if you want to include it in the JOIN then it looks like you have to add a where to the JOIN
Related
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();
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();
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();
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.
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);