Laravel查询构建器不替换问号 [英] Laravel query builder not replacing question mark

查看:72
本文介绍了Laravel查询构建器不替换问号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行以下查询:

return $this->hasMany('App\Task', 'company')
    ->whereNotIn('id', function($query)
    {
        $query->from('tasks')->join('projects', function($join)
            {
                $join->on('projects.id', '=', 'tasks.project')
                    ->where('projects.status', '=', Project::STATUS_ARCHIVED);
            })
            ->select('tasks.id');
    });

但是,如果我输出整个原始查询,则会得到以下信息:

But if I output the whole raw query I get the following:

select * from `tasks` where `tasks`.`company` = 1 and `id` not in (select `tasks`.`id` from `tasks` inner join `projects` on `projects`.`id` = `tasks`.`project` and `projects`.`status` = ?)

如您在原始查询末尾看到的那样,有一个问号并未替换为实际值,而是被替换为'tasks'.'company' = 1.

As you can see at the end of the raw query there's a question mark that wasn't replaced with the actual value, instead 'tasks'.'company' = 1 was.

推荐答案

我找到了解决此问题的方法,方法是使用

I found a solution to this issue by manually setting the bindings using

->setBindings([Project::STATUS_ARCHIVED]);

这是整个代码段:

return $this->hasMany('App\Task', 'company')
    ->whereNotIn('id', function($query)
    {
        $query->from('tasks')->join('projects', function($join)
            {
                $join->on('projects.id', '=', 'tasks.project')
                    ->where('projects.status', '=', '?');
            })
            ->select('tasks.id')
            ->setBindings([Project::STATUS_ARCHIVED]);
    })
    ->where('status', '=', Task::STATUS_INCOMPLETE);

这篇关于Laravel查询构建器不替换问号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆