【发布时间】: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