Inner Join laravel 5.2

2020-03-31 06:25发布

Fellow Coders,

I'm trying to use an inner join query to get multiple data from my relative tables.. What i'm trying to do is to get the (project_id as company_name - subproject_id as subproject_title). I tried to use the query as I will state below. Also I will post printscreens of my tables.

$values = DB::table('hour_registrations')->join('projects', 'id', '=', 'id')->join('subprojects','id', '=', 'id')->select('projects.*', 'id', 'subprojects.id')->get();

enter image description here

enter image description here

enter image description here

enter image description here

I would love some help with my question as I do not understand what to do now..

1条回答
爷、活的狠高调
2楼-- · 2020-03-31 06:36

I think this should do the trick (if I understood your table structure correctly).

I would encourage you to read the official Laravel documentation on Joins.

$query = DB::table('projects')
           ->join('subprojects', 'projects.id', '=', 'subprojects.project_id')
           ->join('companies', 'projects.company_id', '=', 'companies.id')
           ->select('companies.company_name', 'projects.id', 'subprojects.id', 'subprojects.title')
           ->get();
查看更多
登录 后发表回答