Each member of my website has the ability to be "tagged" with skills. Now, I am trying to create a csv with each row populated with one member's information along with an added column for the name of the skill they have been tagged with.
in other words:
____________________________________
|member 1 name | address | etc | skill|
____________________________________
|member 2 name | address | etc | skill|
I can get everything to work great except for adding the skill to the end of the row. When I try to add it, I get the following error:
Undefined property: Illuminate\Database\Eloquent\Collection::$name
Controller
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("
", $result));
// Force the download.
return Response::download($tmpFile, $name);
}
I am creating my row in the contractor.php model
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
]);
}
If I change $this->skills->name to $this->skills the export works, here is my skill cell output:
[{"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}}]
Might also be helpful to know that I am using "whereHas" to get my collection of members.
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;
}
When I dd($c) inside the nested foreach in my controller, I get a collection with the member info. I also see a relationship dropdown that has the information for the relevant skill.
Anyone know how I get the skill to added to my row? TIA