我网站的每个成员都有能力被标记为"有技巧。现在,我正在尝试创建一个csv,其中每行填充一个成员的信息以及添加了他们已被标记的技能名称的列。
换句话说,:
____________________________________
|member 1 name | address | etc | skill|
____________________________________
|member 2 name | address | etc | skill|
除了将技能添加到行尾之外,我可以使一切工作变得更好。当我尝试添加它时,我收到以下错误:
Undefined property: Illuminate\Database\Eloquent\Collection::$name
控制器
public function memberDirectoryExport()
{
$categories= $this->member->membersDirectoryExport();
//create empty array
$rows = [];
//create header row
$header = [];
//add results to header row... not important
$header[] = $this->getExportCsvTitles();
foreach ($categories as $skill) {
foreach($skill->contractors->sortBy('company_name') as $c){
$rows[]= $c->exportDirectoryCsvRow();
}
}
// merge arrays to sent to file_put_contents
$result = array_merge($header, $rows);
// Get a temp name.
list($name, $tmpFile) = self::_getCsvTmpName();
// Store the contents for Laravel to retrieve.
file_put_contents($tmpFile, implode("\n", $result));
// Force the download.
return Response::download($tmpFile, $name);
}
我在contractor.php模型中创建了我的行
public function exportDirectoryCsvRow()
{
return implode("\t", [
$this->company_name, // 1
$this->contact_firstName, // 2
$this->contact_lastName, // 3
$this->contact_email, // 4
$this->contact_address1, //5
$this->contact_address2, // 6
$this->contact_city, // 7
$this->contact_state, //8
$this->contact_postalCode, // 9
$this->contact_phoneNumber, // 10
$this->contact_fax, // 11
$this->skills->name, // This is the problem
]);
}
如果我将$ this-> skills-> name更改为$ this->技能导出有效,这是我的技能单元格输出:
[{"id":1,"name":"Accounting","created_at":"-0001-11-30 00:00:00","updated_at":"-0001-11-30 00:00:00","deleted_at":null,"pivot":{"contractor_id":462,"skill_id":1}}]
也可能有助于知道我正在使用" whereHas"得到我的成员集合。
public function membersDirectoryExport()
{
$user = Sentry::getUser();
$localKey = $user->local_number;
$skills = Skill::whereHas('members', function ($query) use ($localKey) {
$query->where('local_number', '=', $localKey);
$query->where('status', '=', 'active' );
})->orderBy('name', 'asc')->get();
return $skills;
}
当我在控制器中的嵌套foreach中dd($ c)时,我会收到一个包含成员信息的集合。我还看到一个关系下拉列表,其中包含相关技能的信息。
任何人都知道如何将技能添加到我的行中? TIA