vendredi 15 juillet 2016

Joining two models to obtain data

I query my model like so

$projects = Project::where('status', '!=', 'Completed')->get();

This will return me something like this

#attributes: array:16 [▼
    "id" => "7"
    "user_id" => "32"
    "contactEmail" => "sdfsdf@dsfsdf.com"
    "deploymentDate" => "2016-07-29"
    "status" => "Starting"
    "deleted_at" => null
    "created_at" => "2016-07-12 14:12:32"
    "updated_at" => "2016-07-15 09:47:34"
]

I then pass this model to generate an Excel file

Excel::create('projects', function($excel) use($projects) {
    $excel->sheet('Sheet 1', function($sheet) use($projects) {
        $sheet->fromArray($projects);
    });
})->export('xls');

Everything works fine, and the Excel is generated. One problem I have though is that the excel file shows user_id being 32. Instead of displaying the user_id, I want to display the userName which is part of my Users table.

How can I join these two tables to get the name instead of the id? All relationships are set up correctly.

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire