我在代码中遇到了问题,现在我正在处理一个将所有变量发送到控制器功能的报告页面。到这里为止一切都还好。
我试图在使用paginate()或get()函数之前从此查询中获取一些统计信息。
但是所有结果都会出错。
public function companies(Request $request)
{
$keyword = (isset(\request()->keyword) && \request()->keyword != '') ? \request()->keyword : null;
.
.
.
$sort_by = (isset(\request()->sort_by) && \request()->sort_by != '') ? \request()->sort_by : 'id';
$order_by = (isset(\request()->order_by) && \request()->order_by != '') ? \request()->order_by : 'desc';
$limit_by = (isset(\request()->limit_by) && \request()->limit_by != '') ? \request()->limit_by : '10';
$users = User::with(['user_data', 'status', 'order', 'quotation', 'user_data.country', 'user_data.sector']);
$users = $users->whereHas('roles', function($q){
$q->where('name', 'company');
});
if ($keyword != null){
$users = $users->search($keyword);
}
.
.
.
***// MY PROBLEM HERE***
$users_count = $users->count();
$users_via_count = $users->whereNotNull('via')->count();
$users_normal_classifications = $users->whereHas('user_data', function ($q){
$q->where('company_strength', 0);
})->count();
$users_silver_classifications = $users->whereHas('user_data', function ($q){
$q->where('company_strength', 1);
})->count();
$users_gold_classifications = $users->whereHas('user_data', function ($q){
$q->where('company_strength', 2);
})->count();
dd($users_count, $users_via_count, $users_normal_classifications, $users_silver_classifications, $users_gold_classifications);
***// MY PROBLEM UNTIL HERE***
$users = $users->orderBy($sort_by, $order_by);
$users = $users->paginate($limit_by);
.
.
.
return view('admin.reports.companies', compact('users', ....));
}
dd()的结果:
11980
5818
5815
0
0
但是如果我评论(禁用)($ users_count,$ users_via_count,$ users_normal_classifications,$ users_silver_classifications)
我在$ users_gold_classifications中有一个正确的数字。
我该如何解决这个问题?
关于, 萨米人