jay_gorio
17310
16
Eloquent

How to use Left join in eloquent

Posted 3 years ago by jay_gorio

I want to convert my raw database query into more secure eloquent how do I achieved this? Here is my code:

    $results = CrimeReport::query()

        ->leftjoin('crime_types as ct','ct.id', '=', 'crime_reports.crime_type_id')
        ->leftjoin('crime_name as cn','cn.id', '=', 'crime_reports.crime_name_id')
        ->leftjoin('suspect_profile as s','s.id', '=', 'crime_reports.suspect_id')
        ->leftjoin('victim_profile as v','v.id', '=', 'crime_reports.victim_id')
        ->get([
            'crime_reports.*', //to get ids and timestamps
            'ct.crime_type as crime_type',
            'cn.crime_description as crime_name',
            's.firstname as suspect_name',
            'v.firstname as victim_name'
        ]);

Please sign in or create an account to participate in this conversation.