简体   繁体   中英

laravel access outer query column inside subquery

I am trying to convert raw sql queries into laravel queries.

Here's the raw query:

select
tsk.id,
   tsk.request_id,
   tsk.sys_index,
   tsk.category_group,
   tsk.category,
   tsk.is_assigned,
   tsk.hash_id 
from
   user_tasks as usr 
   inner join
      unassigned_tasks as tsk 
      on usr.task_id = tsk.id 
where
   usr.assigned_to = 12
AND 
    tsk.product_id NOT IN ( SELECT product_id FROM product_progresses WHERE request_id = tsk.request_id )
AND 
    BINARY hash_id NOT IN ( SELECT hash_id FROM product_match_unmatches WHERE request_id = tsk.request_id AND auto_unmatched_by IS NOT NULL )

The laravel query is:

public function getTasks($assigned_to) {
    /** fetch products assigned to a specific user token,
    *   ignore already matched skus, and links that are auto-unmatched
    **/
    $tasks = DB::table('user_tasks as usr')
        ->join('unassigned_tasks as tsk', 'usr.task_id', '=', 'tsk.id')

        ->select('tsk.id', 'tsk.request_id', 'tsk.sys_index', 'tsk.category_group', 'tsk.category', 'tsk.is_assigned', 'tsk.hash_id')

        ->where('usr.assigned_to', '=', $assigned_to);
    $tasks->whereNotIn('tsk.product_id', function($qs) {
        $qs->from('product_progresses')
            ->select(['product_id'])
            ->where('request_id', '=', 'tsk.request_id')
            ->get();

    });
    $tasks->whereNotIn(DB::raw('BINARY `hash_id`'), function($qs) {
        $qs->from('product_match_unmatches')
            ->select('hash_id')
            ->where('request_id', '=', 'tsk.request_id')
            ->whereNotNull('auto_unmatched_by')
            ->get();
    });

    return $tasks->toSql();

The below query should take tsk.request_id value from outer query, but I think the column value is not passed to it.

Here's the output of toSql() :

SELECT `tsk`.`id`, 
   `tsk`.`request_id`, 
   `tsk`.`sys_index`, 
   `tsk`.`category_group`, 
   `tsk`.`category`, 
   `tsk`.`is_assigned`, 
   `tsk`.`hash_id` 
FROM   `user_tasks` AS `usr` 
       INNER JOIN `unassigned_tasks` AS `tsk` 
               ON `usr`.`task_id` = `tsk`.`id` 
WHERE  `usr`.`assigned_to` = ? 

AND `tsk`.`product_id` NOT IN (SELECT `product_id` 
                                  FROM   `product_progresses` 
                                  WHERE  `request_id` = ?) 
   AND BINARY `hash_id` NOT IN (SELECT `hash_id` 
                                FROM   `product_match_unmatches` 
                                WHERE  `request_id` = ? 
                                       AND `auto_unmatched_by` IS NOT NULL) 

Note the ? inside where clauses.

The resultset is different from the raw and laravel query .

I even tried see the bindings value:

//dd($tasks->getBindings());
$sql = str_replace_array('?', $tasks->getBindings(), $tasks->toSql());
dd($sql);

And on running this raw query, it is outputting the correct result-set.

UPDATE:

On checking the bindings , here's what I found:

array:3 [▼
  0 => 12
  1 => "tsk.request_id"
  2 => "tsk.request_id"
]

Here outer query column is wrapped inside quotes and hence treated as a string.

So maybe where clause is trying to compare request_id with a string rather than the outer column.

If it is so, then how do I make them treat as columns rather than string ?

use DB::raw() where you trying to add value of request_id

Example

AND `tsk`.`product_id` NOT IN (SELECT `product_id` 
                              FROM   `product_progresses` 
                              WHERE  `request_id` = DB::raw('tsk.request_id')) 
whereRaw('pgr.request_id = tsk.request_id');

Solved the string issue.

You should try to remove select() method, in the subquery replace where() method with whereColumn() method and remove get() method:

$tasks = DB::table('user_tasks', 'urs')
    ->join('unassigned_tasks as tsk', 'usr.task_id', '=', 'tsk.id')
    ->where('usr.assigned_to', '=', $assigned_to);

Note: i put the alias 'urs' as second argument (view docs )

$tasks->whereNotIn('tsk.product_id', function($qs) {
    $qs->from('product_progresses')
        ->select(['product_id'])
        ->whereColumn('request_id', 'tsk.request_id');

});

If you want get specific fields, you must specify the fields in get() method:

return $tasks->get(array('tsk.id', 'tsk.request_id', 'tsk.sys_index', 'tsk.category_group', 'tsk.category', 'tsk.is_assigned', 'tsk.hash_id'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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