0

I have a query that work fine but does not work pagination how to convert into eloquent to apply pagination

$projects=DB::select("select * from projects INNER JOIN(select DISTINCT * from (SELECT project_id FROM district_group_project_users where user_id='$id' UNION ALL select id from projects where created_by='$id') as sub ) as sub on sub.project_id=projects.id;");

OR is there any other option to add pagination on the following query? thanks

ismail khan
  • 287
  • 1
  • 3
  • 16

1 Answers1

1

I'm suppose you have Model called DistrictGroupProjectUser & Project .

$subQuery = DistrictGroupProjectUser::select('project_id')
                   ->where('user_id', $id)
                   ->union(Project::select('id')->where('created_by',$id))
                   ->distinct();

$projects = Project::joinSub($subQuery, 'sub', function ($join) {
                 $join->on('projects.id', '=', 'sub.projects_id');
            })->paginate(20);

Based on laravel documentation

anas omush
  • 322
  • 1
  • 7