Link to home
Start Free TrialLog in
Avatar of Eduardo Fuerte
Eduardo FuerteFlag for Brazil

asked on

Could you point how to code an inline if for a select by using Laravel's Eloquent?

Hi Experts


Could you point how to code an inline if for a select by using Laravel's Eloquent?

Accordingly to

->select('users.id',...',
	// This generates an ERROR!	 
		 'if(users.aceiteRegulamento ==1, "Sim", "Não") as aceite','users.created_at','users.updated_at')

Open in new window


SQLSTATE[42S22]: Column not found: 1054 Unknown column 'if(users.aceiteRegulamento ==1, "Sim", "Não")' in 'field list' (SQL: select `users`.`id`, `users`.`login


Thanks in advance
Avatar of RBertora
RBertora
Flag of United Kingdom of Great Britain and Northern Ireland image

You need the Case statement (assuming you are asking about SQL Server SQL)

From https://stackoverflow.com/questions/6608102/sql-server-inline-if-else


select
 case MyFlag
   when 1 then 'YES'
   when 0 then 'NO'
   else 'OOPS'
 end
from MyTable
where it's used just like a switch in C-like languages and the other is:

select
 case
   when MyFlag = 1 then 'YES'
   when MyFlag = 0 then 'NO'
   -- when some unrelated condition...
   else 'OOPS'
 end
from MyTable
Avatar of Eduardo Fuerte

ASKER

Hi

I'm using Laravel Eloquent and had attempt  to use it, also:

'(CASE WHEN users.aceiteRegulamento = "0" THEN "Não" WHEN users.aceiteRegulamento = "1" THEN "Sim" ELSE "Não" END) AS aceiteRegulamento'

Open in new window


But it doesn't run inside the query.
ASKER CERTIFIED SOLUTION
Avatar of RBertora
RBertora
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Rbertora

Perfectly!!!

Thank you for the help!
Hey Eduardo, thank you very much, muchas gracias / Muito obrigado for the positive review!