Laravel set collation for join (SQL server)

Viewed 1627

I'm trying to move plain SQL query to Laravel query builder.

It's not my first time doing this, but now a "different case has popped up".

I need to set collation for join and in plain SQL it's no problem:

"...
LEFT OUTER JOIN " . $pltable1 . " on PL01001=supplier collate Latvian_BIN
..."

Laravel throws error when I try:

->leftJoin($pltable1, "PL01001", "=", "supplier");

Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Latvian_BIN" in the equal to operation.

I know it's because I didn't specify a collation for join but I have no clue of how to do this. Has anyone encountered something like this and found solution?

P.S. I can't change default collation on table itself

1 Answers

I remembered I asked this question and realized I'v found solution to this problem:

DB::table('table1')
    ->leftJoin('table2', 'table1.column', '=', DB::raw('table2.column collate Latvian_BIN'))
    ->get();

Basically all that is required is to wrap one column and collation in DB::raw() Hope this helps to someone someday

Related