Laravel-admin: 文档里数据导出Laravel-Excel v3.*的例子如何导出关联表数据

Created on 24 Apr 2019  ·  12Comments  ·  Source: z-song/laravel-admin

  • Laravel Version: 5.8.9
  • PHP Version: 7.2.11
  • Laravel-admin: 1.6.12

Description:

例子的地址:
https://laravel-admin.org/docs/zh/model-grid-export
我试着加了一个关联表的字段,却无法导出

这个是报表类文件
app\Admin\Extensions\EventDailyExporter.php

<?php

namespace App\Admin\Extensions;

use Encore\Admin\Grid\Exporters\ExcelExporter;

class EventDailyExporter extends ExcelExporter
{
    protected $fileName = '事件日报表.xlsx';

    protected $columns = [
        'id'      => 'ID',
        'belongsToEvent.name'   => '事件ID',
        'mark' => '子标识',
        'date' => '日期',
        'counter' => '计数',
        'created_at' => '添加时间',
        'updated_at' => '更新时间',
    ];

}

这个是模型类文件
app\Models\EventDaily.php

public function belongsToEvent()
    {
        return $this->belongsTo(ModelEvent::class, 'event_id', 'id');
    }

错误提示如下:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'belongsToEvent.name' in 'field list' (SQL: select `id`, `belongsToEvent`.`name`, `mark`, `date`, `counter`, `created_at`, `updated_at` from `event_dailies` order by `date` desc limit 1000 offset 0)

Steps To Reproduce:

Most helpful comment

in columns array should use FOREIGN KEY name

for example :
public function belongsToEvent()
{
return $this->belongsTo(ModelEvent::class, 'event_id', 'id');
}

<?php

namespace App\Admin\Extensions;

use Encore\Admin\Grid\Exporters\ExcelExporter;
use Maatwebsite\Excel\Concerns\WithMapping;

class EventDailyExporter extends ExcelExporter implements WithMapping
{
    protected $fileName = '事件日报表.xlsx';

    protected $columns = [
        'id'      => 'ID',
        'event_id'   => '事件ID',
        'mark' => '子标识',
        'date' => '日期',
        'counter' => '计数',
        'created_at' => '添加时间',
        'updated_at' => '更新时间',
    ];

    public function map($row) : array
    {
        return [
            $row->id,
            data_get($row, 'belongsToEvent.name'),
            ...
        ];
    }
}

All 12 comments

提交了一个修复来解决这个问题,更新laravel-admin到dev-master版本,导出类修改成下面这样:

<?php

namespace App\Admin\Extensions;

use Encore\Admin\Grid\Exporters\ExcelExporter;
use Maatwebsite\Excel\Concerns\WithMapping;

class EventDailyExporter extends ExcelExporter implements WithMapping
{
    protected $fileName = '事件日报表.xlsx';

    protected $columns = [
        'id'      => 'ID',
        'belongsToEvent.name'   => '事件ID',
        'mark' => '子标识',
        'date' => '日期',
        'counter' => '计数',
        'created_at' => '添加时间',
        'updated_at' => '更新时间',
    ];

    public function map($row) : array
    {
        return [
            $row->id,
            data_get($row, 'belongsToEvent.name'),
            ...
        ];
    }
}

爱你,谢谢大大

@z-song 我试着像你说的那样做了,但是现在不会跟据条件导出,都是所有数据的导出
我现在是1.6.15

laravel admin 1.7 拿不到 data_get 的数据。。请问这个功能已经加入到1.7了嘛?

protected $columns = [
        'id'      => 'ID',
        'smatch.title'   => '活动',
        'name' => '姓名',
        'registerRelation.fees' => '费用'
    ];

    public function map($row) : array
    {
        //dd($row);
        return [
            $row->id,
            data_get($row, 'smatch.title'),
            $row->name,
            data_get($row, 'registerRelation.fees')
        ];
    }

data_get的值没有哎。

我也是

protected $columns = [
'id' => 'ID',
'title' => '活动',
'name' => '姓名',
'fees' => '费用'
];

public function map($row) : array
{
    //dd($row);
    return [
        $row->id,
        data_get($row, 'smatch.title'),
        $row->name,
        data_get($row, 'registerRelation.fees')
    ];
}

改成这样就好了,ok

laravel admin 1.7 拿不到 data_get 的数据。。请问这个功能已经加入到1.7了嘛?

protected $columns = [
        'id'      => 'ID',
        'smatch.title'   => '活动',
        'name' => '姓名',
        'registerRelation.fees' => '费用'
    ];

    public function map($row) : array
    {
        //dd($row);
        return [
            $row->id,
            data_get($row, 'smatch.title'),
            $row->name,
            data_get($row, 'registerRelation.fees')
        ];
    }

data_get的值没有哎。

我也是这样, 最后怎么解决的呢

in columns array should use FOREIGN KEY name

for example :
public function belongsToEvent()
{
return $this->belongsTo(ModelEvent::class, 'event_id', 'id');
}

<?php

namespace App\Admin\Extensions;

use Encore\Admin\Grid\Exporters\ExcelExporter;
use Maatwebsite\Excel\Concerns\WithMapping;

class EventDailyExporter extends ExcelExporter implements WithMapping
{
    protected $fileName = '事件日报表.xlsx';

    protected $columns = [
        'id'      => 'ID',
        'event_id'   => '事件ID',
        'mark' => '子标识',
        'date' => '日期',
        'counter' => '计数',
        'created_at' => '添加时间',
        'updated_at' => '更新时间',
    ];

    public function map($row) : array
    {
        return [
            $row->id,
            data_get($row, 'belongsToEvent.name'),
            ...
        ];
    }
}

in columns array should use FOREIGN KEY name

for example :
public function belongsToEvent()
{
return $this->belongsTo(ModelEvent::class, 'event_id', 'id');
}

<?php

namespace App\Admin\Extensions;

use Encore\Admin\Grid\Exporters\ExcelExporter;
use Maatwebsite\Excel\Concerns\WithMapping;

class EventDailyExporter extends ExcelExporter implements WithMapping
{
    protected $fileName = '事件日报表.xlsx';

    protected $columns = [
        'id'      => 'ID',
        'event_id'   => '事件ID',
        'mark' => '子标识',
        'date' => '日期',
        'counter' => '计数',
        'created_at' => '添加时间',
        'updated_at' => '更新时间',
    ];

    public function map($row) : array
    {
        return [
            $row->id,
            data_get($row, 'belongsToEvent.name'),
            ...
        ];
    }
}

这样解决是可以的

in columns array should use FOREIGN KEY name

for example :
public function belongsToEvent()
{
return $this->belongsTo(ModelEvent::class, 'event_id', 'id');
}

<?php

namespace App\Admin\Extensions;

use Encore\Admin\Grid\Exporters\ExcelExporter;
use Maatwebsite\Excel\Concerns\WithMapping;

class EventDailyExporter extends ExcelExporter implements WithMapping
{
    protected $fileName = '事件日报表.xlsx';

    protected $columns = [
        'id'      => 'ID',
        'event_id'   => '事件ID',
        'mark' => '子标识',
        'date' => '日期',
        'counter' => '计数',
        'created_at' => '添加时间',
        'updated_at' => '更新时间',
    ];

    public function map($row) : array
    {
        return [
            $row->id,
            data_get($row, 'belongsToEvent.name'),
            ...
        ];
    }
}

Thanks a lot! It works for me.
May I know why need to include FOREIGN KEY name in columns, not just belongsToEvent.name?

I have a problem with $columns array, PHP array accepts no duplicated key.
example :
protected $columns = [
'id' => 'ID',
'user_id' => 'name',
'user_id' => 'phone',
];
php arrays will remove the duplicated keys , how i can resolve this problem ?

I have a problem with $columns array, PHP array accepts no duplicated key.
example :
protected $columns = [
'id' => 'ID',
'user_id' => 'name',
'user_id' => 'phone',
];
php arrays will remove the duplicated keys , how i can resolve this problem ?

hello:

for example :

use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithHeadings;

class RecordExport extends ExcelExporter implements WithMapping, WithHeadings, ShouldAutoSize

// protected $columns = [
//// 'id' => 'id',
// 'user_id' => 'name',
// 'user_id' => 'phone',
// ];

public function headings(): array
{
    return [
        'id',
        'name',
        'phone',
    ];
}

like this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

taimaiduc picture taimaiduc  ·  3Comments

qcol picture qcol  ·  3Comments

chenyongmin picture chenyongmin  ·  3Comments

MarKco picture MarKco  ·  3Comments

zhenyangze picture zhenyangze  ·  3Comments