2

In Laravel 9 we had raw sql lines such as the following:

...
->whereIn(DB:raw('concat(a_col, b_col)'), function ($query) {
...

In order to be compatible with Laravel 10 we changed such lines to use the getValue method:

...
->whereIn(DB::raw('concat(a_col, b_col)')->getValue(DB::getQueryGrammar()), function ($query) {
...

This worked in most places, however for sql that used functions like concat above it produces sql with backticks e.g:

...
AND `concat(a_col, b_col)` IN (
...

This produces an error as the backticks imply the whole statement is a column: Column not found: 1054 Unknown column 'concat(a_col, b_col)' in 'IN/ALL/ANY subquery'

Here's a Laravel 9 full query build example for context:

$query = DB::table('table_a')
->join('table_b', 'table_b.id', '=', 'table_a.col_c')
->select([
    'table_b.c_id',
    DB::raw('COUNT(DISTINCT(col_c)) AS any_count'),
    DB::raw('COUNT(CASE WHEN col_b = 1 THEN 1 END) AS one_count'),
    DB::raw('COUNT(CASE WHEN col_b = 2 THEN 1 END) AS two_count'),
])
->where('col_d', '=', ColD::class)
->where('col_e', '=', true)
->whereIn(DB::raw('concat(col_a, col_b, col_c)'), function ($query) {
    $query->from('table_a as latest_table_a')
          ->selectRaw('concat(max(col_a), col_b, col_c) as max_unique_cols')
          ->whereColumn('col_d', 'table_a.col_d')
          ->whereColumn('col_c', 'table_a.col_c')
          ->groupBy('col_d', 'col_c', 'col_b');
})
->groupBy('table_b.c_id');
AndyW
  • 985
  • 3
  • 10
  • 21

1 Answers1

0

Have now found out that as long as you are inside a query builder Laravel 10 will handle DB::raw() as before. So I didn't need to change anything!

i.e keep the code like this:

...
->whereIn(DB:raw('concat(a_col, b_col)'), function ($query) {
...
AndyW
  • 985
  • 3
  • 10
  • 21