Я создаю ресурс API, который позволяет экспортировать данные отчетов в формате .xlsx
. Проблема в том, что некоторые данные имеют отношение один ко многим, и я не могу найти документацию для этого.
Структура данных довольно проста, она выглядит следующим образом:
App\User {#103075
id: 1,
company_id: 1,
name: "Test User",
email: "test@prismahr.com",
email_verified_at: null,
created_at: "2020-03-28 17:03:15",
updated_at: "2020-03-28 17:03:15",
educations: Illuminate\Database\Eloquent\Collection {#103046
all: [
App\Education {#103076
id: 1,
user_id: 1,
level: "est",
institution: "reprehenderit",
major: "soluta",
start: "1985-11-08",
end: "1997-09-29",
created_at: "2020-03-28 17:03:38",
updated_at: "2020-03-28 17:03:38",
},
App\Education {#103066
id: 2,
user_id: 1,
level: "fuga",
institution: "et",
major: "facilis",
start: "2018-06-07",
end: "1989-08-13",
created_at: "2020-03-28 17:03:38",
updated_at: "2020-03-28 17:03:38",
},
App\Education {#103087
id: 3,
user_id: 1,
level: "fuga",
institution: "quibusdam",
major: "quidem",
start: "1990-11-24",
end: "2002-02-28",
created_at: "2020-03-28 17:03:38",
updated_at: "2020-03-28 17:03:38",
},
App\Education {#103068
id: 4,
user_id: 1,
level: "eveniet",
institution: "corporis",
major: "tempore",
start: "2000-09-12",
end: "1993-02-10",
created_at: "2020-03-28 17:03:38",
updated_at: "2020-03-28 17:03:38",
},
App\Education {#103042
id: 5,
user_id: 1,
level: "ut",
institution: "modi",
major: "incidunt",
start: "2000-03-12",
end: "1989-02-02",
created_at: "2020-03-28 17:03:38",
updated_at: "2020-03-28 17:03:38",
},
],
},
}
Я уже создал класс Export для этого, но не могу получить объект user
. Пожалуйста, посмотрите на мой сценарий:
<?php
namespace App\Exports\Sheet;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithTitle;
class UsersEducationsSheet implements FromCollection, WithTitle, WithMapping, ShouldAutoSize
{
/**
* We should provide company id so the exporter does not
* export the wrong users data from another company.
*
* @var integer
*/
private $companyId;
/**
* We should check if the user wants the result to includes
* all data from the collection instance or only specific
* users data.
*
* @var boolean
*/
private $exportAll;
/**
* We should provide user id to be used by the exporter
* to determine which users to be exported.
*
* @var integer
*/
private $userId;
/**
* Register all the variables into constructor.
*
* @param integer $companyId
* @param boolean $exportAll
* @param integer $userId
*/
public function __construct(int $companyId = null, bool $exportAll = true, int $userId = null)
{
$this->companyId = $companyId;
$this->exportAll = $exportAll;
$this->userId = $userId;
}
/**
* Define heading for the fields.
*
* @return array
*/
public function headings(): array
{
return [
'#',
'Employee Name',
'Level',
'Institution',
'Major',
'Start Year',
'End Year'
];
}
/**
* Map the collection into a proper orders.
*
* @param object $user
* @return array
*/
public function map($education): array
{
return [
$education->user->name,
$education->level,
$education->institution,
$education->major,
$education->start,
$education->end,
];
}
public function collection()
{
if ($this->exportAll) {
return Education::select('level', 'institution', 'major', 'start', 'end')
->with(['user' => function ($user) {
$user->whereCompanyId($this->companyId);
}])
->get();
}
}
}
Есть идеи?