/**
* @api-学校学生列表
* @param-int|_page|页码|N|
* @param-string|_sort|排序字段|N||created_at
* @param-string|_order|排序方式|N|正序还是倒序,asc或desc|desc
* @param-string|_keyword|搜索关键词name学生名字 |N|
* @param-int|_limit|每页条数|N|
* @param-int|school_id|学校id|N|
* @param-int|grade_id|年级id|N|
* @param-array|birthday|生日筛选|N|
* @param-string|parent_name|家长姓名|N|
* @param-string|parent_email|家长邮箱|N|
* @param-int|class_id|班级id|N|
* @param-int|class_status|0全部 1未分配班级|N|
* @param-int|parent_status|0全部 1未关联家长|N|
* @param-int|status|状态|N|
* @return-array
*/
public function index(Request $request)
{
$schoolId = $request['_user']['school_id'];
$classStatus = $request['class_status'];
$parentStatus = $request['parent_status'];
$gradeId = $request['grade_id'];
$classId = $request['class_id'];
$birthday = $request['birthday'];
$search = $request->input('_keyword');
$list = StudentUser::with(['parent:id,name', 'class:id,name', 'grade:id,name'])
->when($search, function ($query) use ($search) {
$query->where('name', 'like', '%' . $search . '%')->orWhereHas('parent', function ($query) use ($search) {
$query->where('email', 'like', '%' . $search . '%')->orWhere('name', 'like', '%' . $search . '%');
});
})
->when($classStatus, function ($query) {
$query->whereNull('class_id');
})
->when($parentStatus, function ($query) {
$query->whereDoesntHave('parent_student');
})
->when($gradeId, function ($query) use ($gradeId) {
$query->where('grade_id', $gradeId);
})
->when($classId, function ($query) use ($classId) {
$query->where('class_id', $classId);
})
->when($birthday, function ($query) use ($birthday) {
$query->whereBetween('birthday', $birthday);
})->where('status', 2)->where('school_id', $schoolId)
->index($request);
return $this->page($list);
}
多个搜索字段不同表的时候可以使用when加orWhereHas
|