【问题标题】:Missing data when exporting to excel using Laravel Maatwebsite使用 Laravel Maatwebsite 导出到 excel 时缺少数据
【发布时间】:2019-06-03 02:22:50
【问题描述】:

我正在将数据从数据库导出到 Excel 并下载文件。它可以工作,但由于某种原因,“用户名”数据丢失了。有任何想法吗?第一次使用 Laravel Excel。谢谢。

UserExport.php

<?php
namespace App\Exports;
use App\data;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;


class UsersExport implements FromCollection,WithHeadings
{
/**
* @return \Illuminate\Support\Collection
*/
public function collection()
{
    return data::select(['username', 'password', 'email', 'phone'])->get();
}

public function headings(): array
{
    return [
        'Username',
        'Password',
        'Email',
        'Phone'
    ];
}
}

控制器

public function exportExcel()
{
    return Excel::download(new UsersExport, 'users.xlsx');
}

Data.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class data extends Model
{
protected $fillable = ['username','password','email','phone'];

protected $table = 'user';
protected $primaryKey = 'username';
public $timestamps = false;

}

【问题讨论】:

  • 我可以看看你App\data的内容吗? @stalwart1014
  • 这里的data 是什么?
  • @Ohgodwhy 检查编辑
  • @JueViole17 检查编辑
  • 您检查过collection() 方法的输出是否包含数据?如果从collection() 方法返回data::all(),结果是什么?

标签: php laravel vue.js maatwebsite-excel


【解决方案1】:

而不是将所需的列封装在数组中

public function collection()
{
    return data::select(['username', 'password', 'email', 'phone'])->get();
}

试着改成这个

public function collection()
{
    return data::select('username', 'password', 'email', 'phone')->get();
}

【讨论】:

  • 还是一样。
猜你喜欢
  • 2019-05-15
  • 1970-01-01
  • 2021-10-08
  • 2019-05-20
  • 2016-11-23
  • 2019-04-17
  • 2017-03-07
  • 2020-12-20
  • 2016-05-04
相关资源
最近更新 更多