'Converting Sql query to Laravel 8 Query Showing error
Here is my sql query
select name, case
WHEN iso2 = 'DE' THEN 'Germany'
WHEN iso2 = 'CH' THEN 'Switzerland'
ELSE 'India' End
from cities
My Laravel 8 query
DB::table('cities')->select('name as lang_name', 'iso2', DB::raw('(CASE WHEN iso2 = "DE" THEN "Germany" WHEN iso2 = "CH" THEN "Switzerland" ELSE "India" END) AS codeCity'))->get();
Showing error column: 7 ERROR: column "DE" does not exist LINE 1:
Solution 1:[1]
DB::table('cities')->select('name as lang_name', DB::raw('(CASE WHEN iso2 = "DE" THEN "Germany" WHEN iso2 = "CH" THEN "Switzerland" ELSE "India" END) AS codeCity)')->get();
Solution 2:[2]
Try changing from double quotes to single quotes. E.g. in postgresql double quotes is used to identify columns, while singel quotes are used for values. That explanation would explain your error.
DB::table('cities')->select('name as lang_name', 'iso2', DB::raw("(CASE WHEN iso2 = 'DE' THEN 'Germany' WHEN iso2 = 'CH' THEN 'Switzerland' ELSE 'India' END) AS codeCity"))->get();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | amirhosein hadi |
| Solution 2 | tbjorch |
