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.
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.