Inner Join laravel 5.2

2020-03-31 06:23发布

问题:

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();

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

回答1:

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();