Laravel Query Builder Does Not Replace Question Mark

I run the following query:

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 all the raw request, 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` = ?)

As you can see at the end of the raw query, the question mark, which was not replaced by the actual value, was instead 'tasks'.'company' = 1.

+1
source share
3 answers

I found a solution to this problem by manually setting the bindings with

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

Here's the whole snippet:

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);
0
source

You can listen to the event illuminate.query. Before the request, add the following event listener:

use Event;

Event::listen('illuminate.query', function($query, $params, $time)
{
    dd([
        $query,  // prepared statement
        $params, // query params (? symbols will be replaced with)
        $time    // execution time
    ]);
});
+1
source

->get(); .

Try:

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');
    })->get();
0

Source: https://habr.com/ru/post/1671965/


All Articles