The export excel that comes with laravel-admin will export other data associated with this model. So refer to the official documentation to adjust the code
Article table: id, title, user_id
User table: id, username
//The article model is associated with the user
public function user(){
return $this->belongsTo(User::class, 'user_id', 'id');
}
//ExcelExporter.php <?php namespace App\Admin\Extensions; use Encore\Admin\Grid; use Encore\Admin\Grid\Exporters\AbstractExporter; use Maatwebsite\Excel\Facades\Excel; class ExcelExpoter extends AbstractExporter { protected $head = []; protected $body = []; public function setAttr($head, $body){ $this->head = $head; $this->body = $body; } public function export() { Excel ::create('Filename', function ( $excel ) { $excel ->sheet('Sheetname', function ( $sheet ) { // This logic is to extract the fields to be exported from the table data $head = $ this -> head; $body = $this -> body; $bodyRows = collect( $this ->getData())->map( function ( $item ) use ( $body ) { foreach ( $body as $keyName){ $arr[] = array_get($item, $keyName); } return $arr; }); $rows = collect([$head])->merge($bodyRows); $sheet->rows($rows); }); })->export('xls'); } }
Instructions:
$excel = new ExcelExpoter(); $excel->setAttr(['id', '标题', '作者'], ['id', 'title', 'user.username']); $grid->exporter($excel);