【发布时间】:2017-06-16 11:00:41
【问题描述】:
我需要您的帮助,因为我想尽可能优化这些查询...
我想获取以下位置的警报计数:companies -> sites -> interventions -> purchases -> alerts
我想获取以下操作的计数:contractors -> actionscompanies -> actions
我做了以下但每个页面上的 7 个请求并不是很好:/
$companies = Company::where('id', '=', Auth::user()->userable_id)->orWhere('company_id', '=', Auth::user()->userable_id)->pluck('id')->toArray();
$contractors = Contractor::where('id', '=', Auth::user()->userable_id)->orWhere('company_id', '=', Auth::user()->userable_id)->pluck('id')->toArray();
$cp = array_merge($companies, $contractors);
$sites = Site::whereIn('company_id', $companies)->pluck('id')->toArray();
$interventions = Intervention::whereIn('site_id', $sites)->pluck('id')->toArray();
$purchases = Purchase::whereIn('intervention_id', $interventions)->pluck('id')->toArray();
$bubblecounts['alerts'] = Alerts::whereIn('purchase_id', $purchases)->where('status', 0)->whereDate('alert_date', '!=', '0000-00-00 00:00:00')->count();
$bubblecounts['actions'] = Action::whereIn('actionable_id', $cp)->where([['status', 0], ['alert_date', '!=', null]])->count();
你能帮帮我吗?谢谢!
【问题讨论】: