Laravel - 由于某种原因,max 打破了我的查询

标签 laravel eloquent

我有这段代码:

$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'];

}

最佳答案

你们的关系搞砸了。查询试图使用学生表中的列,但学生表在所述查询中不可用,因为它未连接。查看此 fiddle 以查看 SQL 中出现的问题。 http://sqlfiddle.com/#!9/52c96fa/6 最后,如果我理解正确的话,我会这样做:

在 StatusStudent.php(模型)中:

public function student() {
    return $this->hasOne(Student::class, 'id', 'student_id');
}

在 Controller 中:

public function stackoverflowtest() {
    //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 表中获取 Student。

关于Laravel - 由于某种原因,max 打破了我的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71726763/

相关文章:

php - 我应该使用Put、Patch 和Delete 吗?我正在读一些东西,这让我想知道使用它们是否符合惯例

laravel - 在流明注册立面和服务提供商的位置

mysql - 最爱笑话的餐 table 星座

Laravel Eloquent With() With()

php - 将 'descendants' 关系更改为 'siblings' 关系

javascript - 无法从数据库中检索 ID

php - 如何从 laravel 中的另一个数据库表中检索值?

php - SQLSTATE[23000] : Integrity constraint violation: 1052. Laravel 连接表时的 Eloquent 问题

laravel - 在Laravel中将ID列添加到数据透视表有什么好处?

php - 使用查询生成器更新时 Laravel 模型观察者类不起作用