1

I have the following SQL query. I want to implement the same query in laravel query builder. But no luck. Can help me to how to build this.

SELECT DISTINCT IF(follower_id = 1, leader_id, follower_id ) as user_id,U.email,T.name FROM `followers` as F JOIN users as U ON IF(follower_id = 1, U.id = F.leader_id,U.id=F.follower_id ) JOIN timelines as T ON U.timeline_id = T.id   where F.follower_id = 1 OR F.leader_id =1  

I don't how to add this condition

ON IF(follower_id = 1, U.id = F.leader_id,U.id=F.follower_id )

in Laravel query builder.

1 Answer 1

1

I have not tested but something like this is maybe what you are looking for:

<?php

DB::connection('my_connection')
  ->table('followers AS F')
  ->select([
    'DISTINCT IF(F.follower_id = 1, F.leader_id, F.follower_id) AS user_id',
    'U.email',
    'T.name',
  ])
  ->leftJoin(
    'users AS U', function ($join) {
      $join->on('U.id'. '=', 'F.leader_id')->where('F.follower_id', '=', 1);
      $join->on('U.id', '=', 'F.follower_id')->where('F.follower_id', '<>', 1);
    }
  )
  ->leftJoin(
    'timelines AS T', 'T.id', '=', 'U.timeline_id'
  )
  ->where([
    ['F.follower_id', '=', 1],
    ['F.leader_id', '=', 1]
  ]);
Sign up to request clarification or add additional context in comments.

Comments

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.