gpt4 book ai didi

php - 拉拉维尔 5.4 : Converting a raw SQL query in Laravel Eloquent

转载 作者:行者123 更新时间:2023-11-29 17:38:33 25 4
gpt4 key购买 nike

我正在尝试重写此 SQL 查询,但我陷入了困境

该查询旨在通过使用子查询仅连接最新条目来将项目表连接到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

致:

$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();

我认为这行有问题,但我不知道如何写

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

ErrorException (E_ERROR) strtolower() 期望参数 1 为字符串,给定的对象为\vendor\laravel\framework\src\Illuminate\Database\Grammar.php

最佳答案

使用where():

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

关于php - 拉拉维尔 5.4 : Converting a raw SQL query in Laravel Eloquent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50122426/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com