MYSQL QUERY multiple tables count - mysql

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.

Related

Laravel eloquent double join returning empty

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

Laravel 5.4 Raw Join Query

I have a table TBL_POST used to store blog posts. A post can be assigned to multiple categories, there is a column, cat_id that stores category ID's in comma separated pattern like 2,4,6. I want to use FIND_IN_SET() method in this line
->leftJoin(TBL_CAT.' as c', 'p.cat_id', '=', 'c.id')
to show the associated category names. How can I do that?
public static function getPostWithJoin($status="")
{
$query = DB::table(TBL_POST .' as p')
->select('p.id','p.post_title','p.post_status','u.name as author','c.name as cat_name','p.updated_at')
->leftJoin(TBL_ADMINS.' as u', 'p.post_author', '=', 'u.id')
->leftJoin(TBL_CAT.' as c', 'p.cat_id', '=', 'c.id')
->where('p.post_type','post');
if($status!="all") {
$query->where('p.post_status',$status);
}
$query->orderby('p.id','DESC');
$data = $query->paginate(20);
return $data;
}
You can use callback to create more complicated join query.
->leftJoin(TBL_CAT, function($query){
$query->on(TBL_CAT.'id', '=', 'p.cat_id')->where("**", "**", "**");
})
Here is link on laravel doc - https://laravel.com/docs/5.4/queries#joins "Advanced Join Clauses" section.
UPD::
As mentioned in comment it is not good idea to have string for such types of data. Cause search by equality should be much simpler than string check. Even if your amount of data should not have big difference, you never know what will happen with your app in future.
But if you still want to do that i think you can try like this
->leftJoin(TBL_CAT, function($query){
$query->where(DB::raw("FIND_IN_SET(".TBL_CAT.".id, p.cat_id)"), "<>", "0");
})
Join that will check existence of id in cat_id.

Laravel SUBSTRING on Eloquent query

Question
How do I put a limit on one of the rows in an Eloquent result?
Scenario
I need to retrieve only around 100 characters from one of the fields in my result. Currently I'm using a join statement so multiple results are being returned. I basically need only the first 100 characters from post.content
Code
public function getAll()
{
return Post::select('posts.id', 'posts.title', 'posts.content', 'posts.views', 'posts.comments', 'posts.tags', 'posts.date_created', 'users.image_url', 'users.username', 'users.toxick')
->join('users', 'posts.user_id', '=', 'users.id')
->get();
}
I'm not sure how to go about putting a filter on the query to only return 100 characters. I've looked around briefly but I've not found anything useful, not to my specific scenario at least.
Cant test this at the moment (sorry) but what about:
public function getAll(){
$query = Post::select('posts.id', 'posts.title', 'posts.content','posts.views', 'posts.comments', 'posts.tags', 'posts.date_created', 'users.image_url', 'users.username', 'users.toxick')
->join('users', 'posts.user_id', '=', 'users.id')
->get();
foreach($query as $entries){
$entries->content = substr($entries->content, 1, 100);
}
return $query;
}

How add to collection only models with no record joined - Eloquent

In my model Questions I have simple relation to Standpoint
public function standpoints_byrel()
{
// return $this->hasMany('App\Models\Standpoint');
return $this->hasMany('App\Models\Standpoint', 'question_id');
}
Now,
I have yet another model Userattitude (tableuser_attitudes`) which allow users to upvote and downvote Standpoints.
I am able to list Standpoints, which were voted by a given user:
$user_attitudes = Userattitude::join('entitystandpoints', function ($q) use($questionid,$user) {
$q->where('user_attitudes.item_type', '=', 'entitystandpoint');
$q->on('user_attitudes.item_id', '=', 'entitystandpoints.id');
$q->where('entitystandpoints.question_id', '=', $questionid);
$q->where('user_attitudes.creator_id','=', $user);
})
->select('user_attitudes.*')
->get();
TO DO
Now I try to list all standpoints, which were NOT voted by the given user.
I have no idea how to do it using Eloquent.
Any help appreciated.
edit
condition to meet:
if an user votes up or down, a new model Userattitude is created. Therefore Standpoint models not down- or upvoted have nothing to join. still, in the Userattitude there are two fields for upvoting : 'attitude' and 'importance'. often one of them is null
Try with a left join where the left parameter of the join is null.
Something like this (but please check the syntax out, I'm not an Eloquent expert):
$user_attitudes = Userattitude::leftJoin('entitystandpoints', function ($q) use($questionid,$user) {
$q->where('user_attitudes.item_type', '=', 'entitystandpoint');
$q->on('user_attitudes.item_id', '=', 'entitystandpoints.id');
$q->where('entitystandpoints.question_id', '=', $questionid);
$q->where('user_attitudes.creator_id','=', $user);
})
->whereNull('entitystandpoints.id')
->select('user_attitudes.*')
->get();
Let me know.

Limit JOIN to 1

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