drazvzi741287 2016-06-13 21:25
浏览 29

将Laravel多对多关系导出到csv

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

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大