Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

babuyadhu's avatar

Convert Sql Query to Laravel 8 Query

Help me to convert sql query to Laravel 8

select name, case
WHEN iso2 = 'DE' THEN 'Germany' 
WHEN iso2 = 'CH' THEN 'Switzerland' 
ELSE 'India' End
from cities

When I convert

DB::table('cities')->select('id as city_id', 'name as lang_name', 'ascii_name as city_name', 'iso2', DB::raw('(CASE WHEN iso2 = "DE" THEN "Germany" WHEN iso2 = "CH" THEN "Switzerland" ELSE "India" END) AS codeCity'))->get();  
			````

It shows error
column "DE" does not exist LINE 2
0 likes
2 replies
SilenceBringer's avatar

@babuyadhu can you check the query from query builder

dd(
	DB::table('cities')->select('id as city_id', 'name as lang_name', 'ascii_name as city_name', 'iso2', DB::raw('(CASE WHEN iso2 = "DE" THEN "Germany" WHEN iso2 = "CH" THEN "Switzerland" ELSE "India" END) AS codeCity'))->__toSql()
);  

or by using one of methods https://laravel.com/docs/9.x/queries#debugging

1 like
babuyadhu's avatar

@SilenceBringer I changed Like this , Working now

$c_list = DB::table('cities')
             ->select('id as city_id', 'name as lang_name', 'ascii_name as city_name', 'iso2', DB::raw("case
WHEN iso2 = 'DE' THEN 'Germany' 
WHEN iso2 = 'CH' THEN 'Switzerland' 
ELSE 'India' End AS codeCity"))
             
             ->get();
             print_r($c_list);

Please or to participate in this conversation.