简体   繁体   中英

Missing data when exporting to excel using Laravel Maatwebsite

I'm exporting data from database to excel and downloading the file. It works but for some reason the "username" data is missing. Any ideas? First time using Laravel Excel. Thanks.

数据用户表图像

下载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'
    ];
}
}

Controller

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;

}

Instead of encapsulating your desired columns in an array

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

Try changing it into this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM