Further research got me this far using Tinkerwell.
$agreements = DB::select( DB::raw("SELECT caf.id, cmp.company_code, cmp.legal_name, cmp.city, cmp.state, cmp.country, cmp.denied_party_checked, cmp.denied_party_cleared, cmp.last_denied_party_check, cmp.phone, caf.status_id, caf.reporter, caf.reporter_email, con.accounting_name, con.accounting_email FROM company_customer_agreements caf LEFT JOIN companies cmp ON caf.company_id = cmp.id LEFT JOIN company_contacts con ON cmp.id = con.company_id WHERE cmp.company_code IS NOT NULL ORDER BY cmp.legal_name"));
$cafs = CompanyCustomerAgreement::select('id', 'status_id', 'reporter', 'reporter_email')->get();
however if I try to add the company relationship
$cafs = CompanyCustomerAgreement::with('company')->select('id', 'status_id', 'reporter', 'reporter_email')->get();
I get
Illuminate\Database\Eloquent\Collection {#1696
all: [
App\Models\CompanyCustomerAgreement {#1705
id: 1,
status_id: 3,
reporter: "Jon Gravois",
reporter_email: "[email protected]",
company: App\Models\Company {#1708},
},
and with
$cafs = CompanyCustomerAgreement::with('company:id, legal_name')->select('id', 'status_id', 'reporter', 'reporter_email')->get();
I get
Aliasing 'CompanyCustomerAgreement' to 'App\Models\CompanyCustomerAgreement' for this Tinker session.
Illuminate/Database/QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column ' legal_name' in 'field list' (SQL: select `id`, ` legal_name` from `companies` where 0 = 1)'