简体   繁体   English

DB原始使用eloquent关系Laravel

[英]DB raw use eloquent relationship Laravel

registrant注册人

id
branch_id
name

branch分支

id
branch_code

employee员工

id
registrant_id
name
status

i want to "count" status that is "Actived" by branch, and do clustering by branch_code.我想通过分支“计算”“激活”状态,并通过分支代码进行聚类。 I've made code like this, it didn't work.我做了这样的代码,它没有工作。 maybe friends here can handle it.也许这里的朋友可以处理它。 thanks.谢谢。

Employee::where('status', 'Actifed')
          ->whereRelation('registrant.branch', 'branch_code', $user->branch->branch_code)
          ->select(DB::raw('count(status) as count, registrant.branch.branch_code'))
          ->groupBy('registrant.branch.branch_code')
          ->get();

you can use withCount() and also have your conditions,你可以使用withCount()也有你的条件,

i can't what actually your query must do, but this works to get counts with conditions.我不能实际您的查询必须做什么,但这可以根据条件进行计数。

Model::withCount(['relation' => function($q){
 $q->where('status','=','active');
]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM