0

I need to convert the following sql query to eloquent query builder.

select * from projects
inner join assigned_projects on projects.id = assigned_projects.project_id
where assigned_projects.user_id=9;

where projects and assigned_projects are in many to many relation. Please help me figure it out.

1 Answer 1

1
$project = DB::table('projects')
             ->join('assigned_projects' , 'projects.id' , '=', 'assigned_projects.project_id')
             ->where('assigned_projects.user_id', '=', 9)
             ->get();
Sign up to request clarification or add additional context in comments.

2 Comments

I came across with this one : DB::table('projects') ->join('assigned_projects', function ($join) { $join->on('projects.id', '=', 'assigned_projects.project_id') ->where('assigned_projects.user_id', '=', Auth::user()->id); }) ->get(); But your solutions also do the job done right. Thanks !
Another question, I need to access client name from $project like $project->client->username . But that solution is not working in that case. How will I do that ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.