如何在 laravel 中过滤付费或不付费学生

How to Filter Paid or Not Paid Student In laravel

我刚刚为学校创建了一些支付系统,我需要将我的数据过滤到 PaidNot Paid 类别。

$queryBuilder = Student::query()
    ->leftJoin('fee_scheem_maps', 'students.students_id', '=', 'fee_scheem_maps.sfc_student_id')
    ->leftJoin('fee_scheems', 'fee_scheem_maps.sfc_feescheem_id', '=', 'fee_scheems.fee_scheems_id')
    ->leftJoin('individual_fee_scheems', 'fee_scheem_maps.sifc_feescheem_id', '=', 'individual_fee_scheems.ifs_id')
    ->leftJoin('fee_groups', 'fee_scheems.fee_scheems_id', '=', 'fee_groups.fg_fee_scheem_id')
    ->leftJoin('school_fee_collectors', 'students.students_id', '=', 'school_fee_collectors.fc_student_id')
    // ->Where('school_fee_collectors.fc_fee_group_id', $fromDate)
    // ->orWhereNull('school_fee_collectors.fc_fee_group_id', $pstatus)
    // ->orWhereNull('school_fee_collectors.fc_fee_group_id', $fromDate)
    ->when($fromDate, function ($query) use ($fromDate) {
        return $query->where('school_fee_collectors.fc_fee_group_id', $fromDate);
    })
    ->select('*', DB::raw('count(students_id) as total'));

一个子句工作正常:在我的情况下,我需要随机选择 wherenotWhere...

$queryBuilder = Student::query()
    ->leftJoin('fee_scheem_maps', 'students.students_id', '=', 'fee_scheem_maps.sfc_student_id')
    ->leftJoin('fee_scheems', 'fee_scheem_maps.sfc_feescheem_id', '=', 'fee_scheems.fee_scheems_id')
    ->leftJoin('individual_fee_scheems', 'fee_scheem_maps.sifc_feescheem_id', '=', 'individual_fee_scheems.ifs_id')
    ->leftJoin('fee_groups', 'fee_scheems.fee_scheems_id', '=', 'fee_groups.fg_fee_scheem_id')
    ->leftJoin('school_fee_collectors', 'students.students_id', '=', 'school_fee_collectors.fc_student_id')
    ->when($fromDate, function ($query) use ($fromDate) {
        return $query->where('school_fee_collectors.fc_fee_group_id', $fromDate);
    })

->where(function($query) use ($request){
    if($request->paid_status){
       $query->where('table_name.columName',$request->paid_status)
    }
})->select('*', DB::raw('count(students_id) as total'));

你可以像这样使用 where ,当你的 $request->paid_status 为 null 时意味着没有关于付费未付费时间查询的过滤器 return 一切但什么时候有过滤器时间只有这个在工作

->where(function($query) use ($request){
    if($request->paid_status){
       $query->where('table_name.columName',$request->paid_status)
    }
})