There is a request builder:

DB::table(DB::raw('first_table ft')) ->join(DB::raw('second_table st'), 'st.id', '=', 'ft.second_id') ->get([ 'ft.id', 'IF(st.condition IS NULL,0,1) as is_cond', ]); 

Which gives an SQL error:

 SQLSTATE[HY000]: General error: 4104 General SQL Server error: Check messages from the SQL Server [4104] 

Due to the fact that the received request is as follows:

 SELECT [ft].[id], [IF(st].[condition] AS [NULL,0,1)] ... 

How can I insert a similar condition in the builder?

    1 answer 1

    Wrap this condition in Illuminate\Database\Query\Expression :

     use Illuminate\Database\Query\Expression; // ... DB::table(DB::raw('first_table ft')) ->join(DB::raw('second_table st'), 'st.id', '=', 'ft.second_id') ->get([ 'ft.id', new Expression('IF(st.condition IS NULL,0,1) as is_cond'), ]); 

    judging by backtracking through githab - should work.

    • Thanks for the answer. Not to insert use it is possible to use DB :: raw (), but it did not help. Apparently the problem lies deeper. I will dig further. - GotFly
    • Laravel version is what? - etki
    • 3. Yes, I had to write it right away. - GotFly
    • Maybe worth upgrading? For the third version, I could not even find a tag on the githaba. - etki
    • I do not think that a working project can be safely transferred from 3 to 4/5 version. - GotFly