如何在Laravel过滤器后动态更改计数



我有统计数据部分,我想显示在数据表中记录的计数,有过滤选项之后,数据表记录得到过滤,我想显示统计数据部分应该更新为数据表被过滤。例如,如果Datatable显示100条记录,那么统计数据中的计数应该是100,如果过滤了8条记录,那么统计数据应该动态地更改为8。我有什么办法能做到吗?目前我能够获得计数,但在应用过滤器后它不会改变。

这是我的控制器代码:
public function index(Request $request,ExportDataTable $dataTable)
{
if(request()->ajax())
{
if(!empty($request->BusinessUnit) || !empty($request->CurrOrg1)  || !empty($request->CurrOrg2)  || !empty($request->Role)  || !empty($request->Region)  || !empty($request->ProfessionalClassification)  || !empty($request->Gender)  || !empty($request->CurrOrg3)  || !empty($request->CurrOrg4)  || !empty($request->CurrOrg5))
{             
$loged_user =Auth::user();
$user= DB:: table('users')
->selectRaw('users.*, REPLACE(Org2, "Admin", "") AS ROrg2, REPLACE(Org1, "Admin", "") AS ROrg1, REPLACE(Org3, "Admin" , "") AS ROrg3')
->where('email',$loged_user->email)
->first();
$query= DB:: table('predictions')
->where('CurrOrg1','like','%'.$user->ROrg1.'%')
->where('CurrOrg2','like','%'.$user->ROrg2.'%')
->where('CurrOrg3','like','%'.$user->ROrg3.'%')
->select('predictions.id','predictions.EmployeeNumber','predictions.Role','predictions.Region','predictions.CurrOrg1','predictions.CurrOrg2','predictions.RiskZone','predictions.Probablity','predictions.Rating','predictions.Feedback','predictions.Gender','predictions.CurrOrg3','predictions.CurrOrg4','predictions.CurrOrg5','predictions.ProfessionalClassification','predictions.Local_Expat','predictions.EmployeeRoleSeniority','predictions.FeedRisklevel','predictions.Feedinfluencers','predictions.Action','predictions.Fname','predictions.Lname','predictions.Avgweekhr');
if(!empty($request->CurrOrg2)) {
$query->whereIn('CurrOrg2',$request->CurrOrg2);
}
if(!empty($request->Region)) {
$query->whereIn('Region',$request->Region);
}
if(!empty($request->ProfessionalClassification)) {
$query->whereIn('ProfessionalClassification',$request->ProfessionalClassification);
}
if(!empty($request->CurrOrg1)) {
$query->whereIn('CurrOrg1',$request->CurrOrg1);
}
if(!empty($request->CurrOrg3)) {
$query->whereIn('CurrOrg3',$request->CurrOrg3);
}
if(!empty($request->CurrOrg4)) {
$query->whereIn('CurrOrg4',$request->CurrOrg4);
}
if(!empty($request->CurrOrg5)) {
$query->whereIn('CurrOrg5',$request->CurrOrg5);
}
if(!empty($request->Role)) {
$query->whereIn('Role',$request->Role);
}
if(!empty($request->BusinessUnit)) {
$query->whereIn('BusinessUnit',$request->BusinessUnit);
}
if(!empty($request->Gender)) {
$query->whereIn('Gender',$request->Gender);
}
$data = $query->get();    
}       
else
{
$loged_user =Auth::user();
$user= DB:: table('users')
->selectRaw('users.*, REPLACE(Org2, "Admin", "") AS ROrg2, REPLACE(Org1, "Admin", "") AS ROrg1, REPLACE(Org3, "Admin" , "") AS ROrg3')
->where('email',$loged_user->email)
->first();
$data1 =DB:: table('predictions')
->where('CurrOrg1','like','%'.$user->ROrg1.'%')
->where('CurrOrg2','like','%'.$user->ROrg2.'%')
->where('CurrOrg3','like','%'.$user->ROrg3.'%')
->select('predictions.id','predictions.EmployeeNumber','predictions.Role','predictions.Region','predictions.CurrOrg1','predictions.CurrOrg2','predictions.RiskZone','predictions.Probablity','predictions.Rating','predictions.Feedback','predictions.Gender','predictions.CurrOrg3','predictions.CurrOrg4','predictions.CurrOrg5','predictions.ProfessionalClassification','predictions.Local_Expat','predictions.EmployeeRoleSeniority','predictions.FeedRisklevel','predictions.Feedinfluencers','predictions.Action','predictions.Fname','predictions.Lname','predictions.Avgweekhr');  
$data = $data1->get();     
}


return datatables()->of($data)
->addColumn('Feedback', function($data)
{   
if($data->Action == 'No')
{
return "<a href='#' style='background-color:#CA0088;color:#fff' class='btn btn-sm Feedback' id='".$data->id."'>Feedback</a>";
}
else
{
return "<a href='#' style='background-color:#00A300;color:#fff' class='btn btn-sm Feedback' id='".$data->id."'>Feedback</a>";
} 
})
->escapeColumns([])
->make(true);
}
$Business_unit_name = DB::table('predictions')
->select('BusinessUnit')
->groupBy('BusinessUnit')
->orderBy('BusinessUnit', 'ASC')
->get();
$CurrOrg1_name = DB::table('predictions')
->select('CurrOrg1')
->groupBy('CurrOrg1')
->orderBy('CurrOrg1', 'ASC')
->get();
$CurrOrg2_name = DB::table('predictions')
->select('CurrOrg2')
->groupBy('CurrOrg2')
->orderBy('CurrOrg2', 'ASC')
->get();
$CurrOrg3_name = DB::table('predictions')
->select('CurrOrg3')
->groupBy('CurrOrg3')
->orderBy('CurrOrg3', 'ASC')
->get();
$CurrOrg4_name = DB::table('predictions')
->select('CurrOrg4')
->groupBy('CurrOrg4')
->orderBy('CurrOrg4', 'ASC')
->get();
$CurrOrg5_name = DB::table('predictions')
->select('CurrOrg5')
->groupBy('CurrOrg5')
->orderBy('CurrOrg5', 'ASC')
->get();
$Role_name = DB::table('predictions')
->select('Role')
->groupBy('Role')
->orderBy('Role', 'ASC')
->get();
$Region_name = DB::table('predictions')
->select('Region')
->groupBy('Region')
->orderBy('Region', 'ASC')
->get();
$Gender_name = DB::table('predictions')
->select('Gender')
->groupBy('Gender')
->orderBy('Gender', 'ASC')
->get();
$ProfessionalClassification_name = DB::table('predictions')
->select('ProfessionalClassification')
->groupBy('ProfessionalClassification')
->orderBy('ProfessionalClassification', 'ASC')
->get();
$Prediction= new Prediction;
$Prediction_list= $Prediction::all();
$Topcorrelators = DB::table('predictions')
->select('Topcorrelators')
->groupBy('Topcorrelators')
->orderBy('Topcorrelators', 'ASC')
->get();
$loged_user =Auth::user();
$user= DB:: table('users')
->selectRaw('users.*, REPLACE(Org2, "Admin", "") AS ROrg2, REPLACE(Org1, "Admin", "") AS ROrg1, REPLACE(Org3, "Admin" , "") AS ROrg3')
->where('email',$loged_user->email)
->first();
$Highrisk= DB:: table('predictions')
->where('CurrOrg1','like','%'.$user->ROrg1.'%')
->where('CurrOrg2','like','%'.$user->ROrg2.'%')
->where('CurrOrg3','like','%'.$user->ROrg3.'%')
->where('RiskZone','High Risk')
->count();
$Lowrisk= DB:: table('predictions')
->where('CurrOrg1','like','%'.$user->ROrg1.'%')
->where('CurrOrg2','like','%'.$user->ROrg2.'%')
->where('CurrOrg3','like','%'.$user->ROrg3.'%')
->where('RiskZone','Low Risk')
->count();
$Records= DB:: table('predictions')
->where('CurrOrg1','like','%'.$user->ROrg1.'%')
->where('CurrOrg2','like','%'.$user->ROrg2.'%')
->where('CurrOrg3','like','%'.$user->ROrg3.'%')
->count();                        
$Action= DB:: table('predictions')
->where('CurrOrg1','like','%'.$user->ROrg1.'%')
->where('CurrOrg2','like','%'.$user->ROrg2.'%')
->where('CurrOrg3','like','%'.$user->ROrg3.'%')
->where('Action','!=','No')
->count();
return view('admin.members.Predictions', compact('Business_unit_name','CurrOrg1_name','CurrOrg2_name','CurrOrg3_name','CurrOrg4_name','CurrOrg5_name','Role_name','Region_name','Gender_name','Topcorrelators','ProfessionalClassification_name','Prediction_list','Highrisk','Lowrisk','Records','Action'));
}

,这是我的观点:

<div class="container-fluid">
<!-- Info boxes -->
<div class="row">
<div class="col-12 col-sm-6 col-md-3">
<div class="info-box">
<span class="info-box-icon bg-info elevation-1"><i class="fas fa-users"></i></span>
<div class="info-box-content">
<span class="info-box-text"># Employees</span>
<span class="info-box-number">
{{ $Records }}
</span>
</div>
<!-- /.info-box-content -->
</div>
<!-- /.info-box -->
</div>
<!-- /.col -->
<div class="col-12 col-sm-6 col-md-3">
<div class="info-box mb-3">
<span class="info-box-icon bg-danger elevation-1"><i class="fas fa-user"></i></span>
<div class="info-box-content">
<span class="info-box-text">High Risk Employees</span>
<span class="info-box-number"> {{ $Highrisk }} </span>
</div>
<!-- /.info-box-content -->
</div>
<!-- /.info-box -->
</div>

如果可能的话,我怎么做呢?

嗨,如果你的所有记录在数据变量这么简单,你可以检查计数在laravel叶片例如{{count($data)}}

或您可以使用分页和获取数据use{{$data->total()}}

谢谢。

最新更新