Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将查询分配给变量,然后执行两个单独的查询?_Php_Laravel_Laravel 5_Eloquent - Fatal编程技术网

Php 将查询分配给变量,然后执行两个单独的查询?

Php 将查询分配给变量,然后执行两个单独的查询?,php,laravel,laravel-5,eloquent,Php,Laravel,Laravel 5,Eloquent,是否可以启动一个雄辩的查询,将其分配给一个变量,然后在两个单独的查询中继续使用该变量,而不会使它们相互冲突。一个简单的例子: $students = $this->student // more query stuff ->where('is_active', 1); $bachelorStudents = $students ->where('course_id', 3) ->get(); $masterStudents = $stu

是否可以启动一个雄辩的查询,将其分配给一个变量,然后在两个单独的查询中继续使用该变量,而不会使它们相互冲突。一个简单的例子:

$students = $this->student
    // more query stuff
    ->where('is_active', 1);

$bachelorStudents = $students
    ->where('course_id', 3)
    ->get();

$masterStudents = $students
    ->where('course_id', 4)
    ->get();
或者我需要做什么:

$bachelorStudents = $this->student
    ->where('course_id', 3)
    ->get();

$masterStudents = $this->student
    ->where('course_id', 4)
    ->get();
我一直认为我能做到前者,但我的一些结果似乎表明我做不到,但我愿意相信,如果你能做到,那么也许我做错了什么。

一旦你做到了这一点:

$students = $this->student->where('is_active', 1);
$stundents
将包含一个带有where子句的查询生成器

如果您这样做:

$bachelorStudents = $students->where('course_id', 3)->get();
您将向
$students
生成器中添加另一个where类,这应该可以按预期工作

但是,当您这样做时:

$masterStudents = $students->where('course_id', 4)->get();
您正在将另一个where clasuse添加到同一个
$students
生成器中,从而导致查询生成器如下所示:

$students->where('is_active', 1)
         ->where('course_id', 3)
         ->where('course_id', 4)
         ->get();
这可能不是您所期望的,因为有两个where子句具有不同的
course\u id

$student
视为每次添加子句时都要修改的对象,这样就可以将其用于渐进式查询构建,但请记住,一旦将子句添加到查询构建器中,对象将被修改,并且该子句将保留在构建器中,因此,当您重新使用生成器时,它将包含您先前添加的所有类别

另外,请记住,当您需要对查询应用一些预定义的筛选器时,在Laravel中,您应该在呼叫时使用

$students = $this->student->where('is_active', 1);
您正在创建查询生成器对象。在此对象上调用where*(),通过添加给定条件来更新对象。因此,不可能在第一个代码段中实现您想要的内容,因为当您调用

$masterStudents = $students
  ->where('course_id', 4)
  ->get();

查询生成器已经包含where('course_id',3)约束,当您单身学生时添加了

当每个人都在解释查询生成器及其工作原理时,下面是您的答案

1) 开始你的工作

2) 将初始查询生成器添加到单独的查询中

$bachelorStudentsQuery = clone $studentsQuery;
$masterStudentsQuery = clone $studentsQuery;
3) 指定
where
条件并获得结果

$bachelorStudentsResult = $bachelorStudentsQuery->where('course_id', 3)
                        ->get();

$masterStudentsResult = $masterStudentsQuery->where('course_id',4)
                        ->get();
您的用例对于克隆来说太简单了。
当执行了大量的方法链接时,它可能会帮助您干燥代码,特别是在将过滤器应用于查询时。

所以我需要做我问题中后一个代码段中的工作?所以我需要做我问题中后一个代码段中的工作?在这种特定情况下,是的。我建议你看看,这将使你能够对你的模型应用默认的预过滤器(即获取所有活动用户),并在其上应用更多过滤器为什么我在这个问题上被否决?我不是说我不应该这样做,但至少给我一些解释,这样我也许可以从错误中吸取教训。谢谢
$bachelorStudentsResult = $bachelorStudentsQuery->where('course_id', 3)
                        ->get();

$masterStudentsResult = $masterStudentsQuery->where('course_id',4)
                        ->get();