使用Laravel Maatwebsite导出到excel时丢失数据

时间:2019-06-03 02:22:50

标签: php laravel vue.js maatwebsite-excel

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

Data User table image

Downloaded excel image

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;

}

1 个答案:

答案 0 :(得分:0)

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

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

尝试将其更改为此

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