2

I am trying to convert Sql query to laravel query . Daytabase used is postgresql

Laravel Query

$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"))
             ->whereIn('iso2', array('DE', 'IN', 'FR', 'TA', 'CH'))
             ->orderBy('codeCity','asc')
             ->orderBy('city_name','asc')
             ->get();

Showing error

SQLSTATE[42703]: Undefined column: 7 ERROR: column "codeCity" does not exist LINE 1:

Output is getting when orderby codeCity is removed

DataBase Schema

enter image description here

Without orderby results are getting

$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"))
             ->whereIn('iso2', array('DE', 'IN', 'FR', 'TA', 'CH'))
             ->get();
4
  • 1
    post your table schema. Commented May 23, 2022 at 7:48
  • 1
    @Raptor Table Schema image added. Output is getting when orderby codeCity is removed Commented May 23, 2022 at 7:59
  • @viettel solutions Still showing error "SQLSTATE[42703]: Undefined column: 7 ERROR: column "codeCity"". Output is getting when orderby codeCity is removed Commented May 23, 2022 at 8:01
  • @Raptor while print output after removing orderBy codecity. the results are getting and codeCity is there in output also Commented May 23, 2022 at 8:41

2 Answers 2

2

You can use selectRaw() instead of DB:raw():

DB::table('cities')
         ->select('id as city_id', 'name as lang_name', 'ascii_name as city_name', 'iso2') 
         ->selectRaw("(CASE 
                        WHEN iso2 = 'DE' THEN 'Germany' 
                        WHEN iso2 = 'CH' THEN 'Switzerland' 
                        ELSE 'India' 
                        END) 
                        AS codeCity")
         ->whereIn('iso2', array('DE', 'IN', 'FR', 'TA', 'CH'))
         ->orderBy('codeCity','asc')
         ->orderBy('city_name','asc')
         ->get();

EDIT (for reply): So weird. But you can try another like this

DB::table('cities')
     ->select('id as city_id', 'name as lang_name', 'ascii_name as city_name', 'iso2') 
     ->whereIn('iso2', array('DE', 'IN', 'FR', 'TA', 'CH'))
     ->orderByRaw("CASE 
                    WHEN iso2 = 'DE' THEN 'Germany' 
                    WHEN iso2 = 'CH' THEN 'Switzerland' 
                    ELSE 'India' 
                    END",'asc')
     ->orderBy('city_name','asc')
     ->get();
Sign up to request clarification or add additional context in comments.

2 Comments

Still showing error "SQLSTATE[42703]: Undefined column: 7 ERROR: column "codeCity"". Output is getting when orderby codeCity is removed
@SatheeshKumar We reply in our answer (The comment is a little difficult to write code). You can try it.
0

Please try the query without orderBy statements and check if the output collection is empty or not. One reason for this error could be an empty collection as a result. So make sure the query has any results.

3 Comments

Works fine without orderby. But I need Orderby
Would you please post your query output without orderBy?
Question updated pls check

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.