简体   繁体   English

Laravel 5.4:在Laravel Eloquent中转换原始SQL查询

[英]Laravel 5.4: Converting a raw SQL query in Laravel Eloquent

I'm trying to rewrite this SQL query but I'm stuck at this point 我正在尝试重写此SQL查询,但此时此刻我被困住了

The query is meant to join the projects table to the project_progress table by using a sub-query to only join on the latest entry 该查询旨在通过使用子查询仅将最新条目联接到project_progress表中

SELECT * FROM projects
JOIN project_progress ON project_progress.id = 
(
    SELECT id FROM project_progress
    WHERE project_progress.project_id = projects.id
    ORDER BY project_progress.created_at DESC
    LIMIT 1
)
WHERE project_progress.next_action_date < NOW()
AND projects.status != 'Complete'
AND projects.member_id = 1
ORDER BY projects.title ASC

To: 至:

$projects = App\Project::where('member_id', 1)
    ->join('project_progress', function ($join) {
        $join->on('project_progress.id', '=', function ($query) {
            $query->select('project_progress.id')
                ->from('project_progress')
                ->where('project_progress.project_id', 'projects.id')
                ->orderBy('project_progress.created_at', 'desc')
                ->limit(1);
        });
    })
    ->where('project_progress.next_action_date', '<', Carbon\Carbon::now())
    ->notCompleted()
    ->orderBy('projects.project_title', 'asc')
    ->get();

I think some thing is wrong with this line but I'm not sure how to write it 我认为此行有些错误,但我不确定如何编写

$join->on('project_progress.id', '=', function ($query) {

ErrorException (E_ERROR) strtolower() expects parameter 1 to be string, object given \\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Grammar.php ErrorException(E_ERROR)strtolower()期望参数1为字符串,对象指定为\\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Database \\ Grammar.php

使用where()

$join->where('project_progress.id', '=', function ($query) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM