Php 用于预测的带有where子句的laravel

Php 用于预测的带有where子句的laravel,php,mysql,laravel,where-clause,Php,Mysql,Laravel,Where Clause,我有三张桌子 学生(有多个注册) 程序(具有多个寄存器) 注册(属于许多学生和课程) 我想找到一个学生谁预计通过该计划,而用户不必首先注册该计划。我知道这是一个错误的条件或地方,但我不知道这样做 public function prediction($id) { $program = ReqProgram::find($id); $student= Student::where(function($query) use($program){ $query

我有三张桌子

学生(有多个注册)

程序(具有多个寄存器)

注册(属于许多学生和课程)

我想找到一个学生谁预计通过该计划,而用户不必首先注册该计划。我知道这是一个错误的条件或地方,但我不知道这样做

public function prediction($id)
{
    $program = ReqProgram::find($id);
    $student= Student::where(function($query) use($program){
            $query ->where('score', '>=', $program->score)
            ->orWhere('score', '=', NULL);})
            ->where(function($query) use($program){    
                $query->where('religion', '=', $program->religion)
                ->orWhere('religion', '=', NULL);})
            ->where(function($query) use($program){
                $query ->where('semester', '<=', $program->semester)
                ->orWhere('semester', '=', NULL);})
            ->get();

    return view('kaprodi/prediksi', compact(['mahasiswa', 'program']));  
}
公共功能预测($id)
{
$program=ReqProgram::查找($id);
$student=student::where(函数($query)use($program){
$query->where('score','>=',$program->score)
->或其中('score','=',NULL);})
->其中(函数($query)使用($program){
$query->where('宗教','=',$program->宗教)
->或where('religation','=',NULL);})
->其中(函数($query)使用($program){

$query->where('sement','=program.score,但是如果program.score=null,那么学生也会被选中。这可能吗?请问,我怎么做?我被困了这么久

这行描述了你想要什么:

    $query ->where('score', '>=', $program->score)
    ->orWhere('score', '=', NULL);})
学生的分数为
NULL
或不低于
$program->score
。但是,这不起作用。您需要使用
is
运算符

    $query ->where('score', '>=', $program->score)
    ->orWhereNull('score'))
我不能说你的宗教标准是否正确


编辑:根据

将函数调用更改为
或wherenull
,是否可能在orWhere中使用类似的
或where(“$program->score,'is',null)
?我知道这是错误的,但你有解决方案来处理我的意思吗?@YosRafel编辑了我的答案,请看一看。
public function prediction($id)
{
    $program = ReqProgram::find($id);
    $student= Student::where(function($query) use($program){
            $query ->where('score', '>=', $program->score)
            ->orWhere('score', '=', NULL);})
            ->where(function($query) use($program){    
                $query->where('religion', '=', $program->religion)
                ->orWhere('religion', '=', NULL);})
            ->where(function($query) use($program){
                $query ->where('semester', '<=', $program->semester)
                ->orWhere('semester', '=', NULL);})
            ->get();

    return view('kaprodi/prediksi', compact(['mahasiswa', 'program']));  
}
    $query ->where('score', '>=', $program->score)
    ->orWhere('score', '=', NULL);})
    $query ->where('score', '>=', $program->score)
    ->orWhereNull('score'))