Hello and thanks for taking some time to help with my question.
I am trying to create a conditional if statement using laravel eloquent model.
A sample of my code is like:
$query = DB::table('example')->select('test1',
DB::raw('IF(test2 = "myText" , test2, FALSE) AS test2_Alias'),
DB::raw('IF(test2 = "myText" , test3, FALSE) AS test3_Alias')
)->first();
The condition works fine and i retrieve my results but i need a different thing. Is there a way to remove completely the false statement so my column name does not appear at all?
So my output will be:
1 array field if `test2` text is not `myText` (test1)
3 array fields if `test2` test is `myText` (test1,test2,test3)
Case-when
did not work either for that because still there is a fallback there.
Union
also does not fit me because i need to have the same number of columns in my select statements which is not something i want as explained above.
Of course as a last solution i can always retrieve all the columns and apply the logic in PHP side but i was hoping that there might be a Mysql Solution for my issue.
Edit to add more info
What i am trying to achieve is based on the value of 1 field to select more columns or exclude them from my output.
See Question&Answers more detail:os