Laravel - 由于某种原因,max 打破了我的查询
Laravel - for some reason, max breaks my query
我有这段代码:
$query = Student::whereHas('statusuri', function($q) use ($status) {
$q->latest('status_student.id')->take(1)
->where('status_id', $status)
->whereNotNull('status_id');
});
它工作正常,但我不一定能得到想要的结果。
我尝试将第一行更改为最大值(这样我就不会过滤所有记录然后限制 1),我只是从头开始获取最高 ID - 如下所示:
$query = Student::whereHas('statusuri', function($q) use ($status) {
$q->max('status_student.id')
->where('status_id', $status)
->whereNotNull('status_id');
});
但是我的查询中断了。
出于某种原因,我得到了这个:
Unknown column 'students.id' in 'where clause' (SQL: select max(`status_student`.`id`) as aggregate from `statusuri` inner join `status_student` on `statusuri`.`id` = `status_student`.`status_id` where `students`.`id` = `status_student`.`student_id`)
为什么在我进行此更改后我的查询中断?
谢谢。
Tables:
students
id bigint(20)
//other non-related data
statusuri
id bigint(20)
nume VARCHAR(255)
status_student
id int(11)
student_id int(10)
status_id int(10)
stare_stagiu_id int(11)
created_at timestamp
updated_at timestamp
来自学生的statusuri()
public function statusuri()
{
return $this->belongsToMany(Status::class, 'status_student')
->withPivot('id', 'data_inceput', 'data_sfarsit', 'document', 'status_id', 'stare_stagiu_id')
->withTimestamps();
}
Status 和 StatusStudent 类
class StatusStudent extends Model
{
protected $table = 'status_student';
protected $fillable = ['id', 'student_id', 'status_id', 'stare_stagiu_id'];
}
class Status extends Model
{
protected $table = 'statusuri';
public $fillable = ['nume'];
}
你们的关系一团糟。该查询正在尝试使用学生 table 的列,但学生 table 在所述查询中不可用,因为它未连接。请参阅此 fiddle 以查看 SQL 中出现的问题。
http://sqlfiddle.com/#!9/52c96fa/6
最后,如果我理解正确的话,我会这样做:
在StatusStudent.php(型号)中:
public function student() {
return $this->hasOne(Student::class, 'id', 'student_id');
}
在控制器中:
public function Whosebugtest() {
//Set teststatus
$status = 1;
//Get the latest status of all users - and if that status is correct, retrieve into array
$latest = DB::select( DB::raw("SELECT max(id) as id, student_id FROM status_student group by student_id"));
$array = [];
foreach ($latest as $l) {
$status_id = StatusStudent::whereId($l->id)->whereStatusId($status)->first();
if ($status_id) {
array_push($array, $status_id);
}
}
//$array now holds all the StatusStudent, simply user ->student to get the student related to said status, example below
if($array) {
dd($array[0]->student);
return $array;
} else {
return 'No match';
}
}
首先,如果状态正确,我们会获取每个用户的所有最新记录。然后,我们只需通过关系从 status_student table 中获取 Student。
我有这段代码:
$query = Student::whereHas('statusuri', function($q) use ($status) {
$q->latest('status_student.id')->take(1)
->where('status_id', $status)
->whereNotNull('status_id');
});
它工作正常,但我不一定能得到想要的结果。
我尝试将第一行更改为最大值(这样我就不会过滤所有记录然后限制 1),我只是从头开始获取最高 ID - 如下所示:
$query = Student::whereHas('statusuri', function($q) use ($status) {
$q->max('status_student.id')
->where('status_id', $status)
->whereNotNull('status_id');
});
但是我的查询中断了。
出于某种原因,我得到了这个:
Unknown column 'students.id' in 'where clause' (SQL: select max(`status_student`.`id`) as aggregate from `statusuri` inner join `status_student` on `statusuri`.`id` = `status_student`.`status_id` where `students`.`id` = `status_student`.`student_id`)
为什么在我进行此更改后我的查询中断?
谢谢。
Tables:
students
id bigint(20)
//other non-related data
statusuri
id bigint(20)
nume VARCHAR(255)
status_student
id int(11)
student_id int(10)
status_id int(10)
stare_stagiu_id int(11)
created_at timestamp
updated_at timestamp
来自学生的statusuri()
public function statusuri()
{
return $this->belongsToMany(Status::class, 'status_student')
->withPivot('id', 'data_inceput', 'data_sfarsit', 'document', 'status_id', 'stare_stagiu_id')
->withTimestamps();
}
Status 和 StatusStudent 类
class StatusStudent extends Model
{
protected $table = 'status_student';
protected $fillable = ['id', 'student_id', 'status_id', 'stare_stagiu_id'];
}
class Status extends Model
{
protected $table = 'statusuri';
public $fillable = ['nume'];
}
你们的关系一团糟。该查询正在尝试使用学生 table 的列,但学生 table 在所述查询中不可用,因为它未连接。请参阅此 fiddle 以查看 SQL 中出现的问题。 http://sqlfiddle.com/#!9/52c96fa/6 最后,如果我理解正确的话,我会这样做:
在StatusStudent.php(型号)中:
public function student() {
return $this->hasOne(Student::class, 'id', 'student_id');
}
在控制器中:
public function Whosebugtest() {
//Set teststatus
$status = 1;
//Get the latest status of all users - and if that status is correct, retrieve into array
$latest = DB::select( DB::raw("SELECT max(id) as id, student_id FROM status_student group by student_id"));
$array = [];
foreach ($latest as $l) {
$status_id = StatusStudent::whereId($l->id)->whereStatusId($status)->first();
if ($status_id) {
array_push($array, $status_id);
}
}
//$array now holds all the StatusStudent, simply user ->student to get the student related to said status, example below
if($array) {
dd($array[0]->student);
return $array;
} else {
return 'No match';
}
}
首先,如果状态正确,我们会获取每个用户的所有最新记录。然后,我们只需通过关系从 status_student table 中获取 Student。