Laravel Excel导出:如何让关联数据显示在同一行?
问题描述
使用Laravel Excel通过视图生成Excel导出时,关联的tickets、EMD、invoices数据会显示在主approval数据的下一行,希望将这些关联数据与主数据显示在同一行中。
原视图代码如下:
<table> <thead> <tr style="font-size: 16px;"> <th>approval_no</th> <th>pnr</th> <th>airline</th> <th>cost</th> <th>collected</th> <th>due_date</th> <th>time_limit</th> <th>notes</th> <th>approve_laststatus</th> <th>customer</th> <th>supplier</th> <th>created_by</th> <th>created_at</th> <th>Assignee</th> <th>Ticket Number</th> <th>Created By</th> <th>created_at</th> <th>EMD Number</th> <th>Created By</th> <th>created_at</th> <th>Invoice Number</th> <th>Created By</th> <th>created_at</th> </tr> </thead> <tbody> @foreach($approvals as $approval) <tr> <td>{{ $approval->approval_no }}</td> <td>{{ $approval->pnr }}</td> <td>{{ $approval->airline->airline }}</td> <td>{{ $approval->cost }}</td> <td>{{ $approval->collected }}</td> <td>{{ date("m/d/Y", strtotime($approval->due_date))}}</td> <td>{{ date("m/d/Y", strtotime($approval->time_limit))}}</td> <td>{{ $approval->notes }}</td> <td>{{ $approval->approve_laststatus}}</td> <td>{{ $approval->customer->customer_name}}</td> <td>{{ $approval->supplier->supplier_name}}</td> <td>{{ $approval->creator->first_name}}</td> <td>{{ date("m/d/Y", strtotime($approval->created_at))}}</td> <td>{{$approval->assignee}}</td> </tr> @if(!empty($approval->tickets)) @foreach($approval->tickets as $ticket) <tr> <td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td> <td>{{ $ticket->ticket_number }}</td> <td>{{ $ticket->creator->first_name}}</td> <td>{{ date("m/d/Y", strtotime($ticket->created_at))}}</td> </tr> @endforeach @endif @if(!empty($approval->emd)) @foreach($approval->emd as $emd) <tr> <td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td> <td>{{ $emd->emd_number }}</td> <td>{{ $emd->creator->first_name}}</td> <td>{{ date("m/d/Y", strtotime($emd->created_at))}}</td> </tr> @endforeach @endif @if(!empty($approval->invoices)) @foreach($approval->invoices as $invoice) <tr> <td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td> <td>{{ $invoice->invoice_number }}</td> <td>{{ $invoice->creator->first_name}}</td> <td>{{ date("m/d/Y", strtotime($invoice->created_at))}}</td> </tr> @endforeach @endif @endforeach </tbody> </table>
解决方案
核心思路是将关联数据直接渲染在主approval行的对应列中,而非单独生成新行。通过拼接多个关联记录的字段值(用换行、逗号等分隔),实现同一行展示所有关联数据。
修改后的视图代码如下:
<table> <thead> <tr style="font-size: 16px;"> <th>approval_no</th> <th>pnr</th> <th>airline</th> <th>cost</th> <th>collected</th> <th>due_date</th> <th>time_limit</th> <th>notes</th> <th>approve_laststatus</th> <th>customer</th> <th>supplier</th> <th>created_by</th> <th>created_at</th> <th>Assignee</th> <th>Ticket Number</th> <th>Created By</th> <th>created_at</th> <th>EMD Number</th> <th>Created By</th> <th>created_at</th> <th>Invoice Number</th> <th>Created By</th> <th>created_at</th> </tr> </thead> <tbody> @foreach($approvals as $approval) <tr> <td>{{ $approval->approval_no }}</td> <td>{{ $approval->pnr }}</td> <td>{{ $approval->airline->airline }}</td> <td>{{ $approval->cost }}</td> <td>{{ $approval->collected }}</td> <td>{{ date("m/d/Y", strtotime($approval->due_date))}}</td> <td>{{ date("m/d/Y", strtotime($approval->time_limit))}}</td> <td>{{ $approval->notes }}</td> <td>{{ $approval->approve_laststatus}}</td> <td>{{ $approval->customer->customer_name}}</td> <td>{{ $approval->supplier->supplier_name}}</td> <td>{{ $approval->creator->first_name}}</td> <td>{{ date("m/d/Y", strtotime($approval->created_at))}}</td> <td>{{$approval->assignee}}</td> {{-- 处理Tickets关联数据 --}} <td>{{ $approval->tickets->pluck('ticket_number')->implode("\n") }}</td> <td>{{ $approval->tickets->pluck('creator.first_name')->implode("\n") }}</td> <td>{{ $approval->tickets->pluck('created_at')->map(fn($date) => date("m/d/Y", strtotime($date)))->implode("\n") }}</td> {{-- 处理EMD关联数据 --}} <td>{{ $approval->emd->pluck('emd_number')->implode("\n") }}</td> <td>{{ $approval->emd->pluck('creator.first_name')->implode("\n") }}</td> <td>{{ $approval->emd->pluck('created_at')->map(fn($date) => date("m/d/Y", strtotime($date)))->implode("\n") }}</td> {{-- 处理Invoices关联数据 --}} <td>{{ $approval->invoices->pluck('invoice_number')->implode("\n") }}</td> <td>{{ $approval->invoices->pluck('creator.first_name')->implode("\n") }}</td> <td>{{ $approval->invoices->pluck('created_at')->map(fn($date) => date("m/d/Y", strtotime($date)))->implode("\n") }}</td> </tr> @endforeach </tbody> </table>
关键说明
- 使用Laravel集合的
pluck()方法提取关联模型的指定字段,再用implode("\n")将多个值用换行符拼接,Excel会自动识别换行并在单元格内换行显示。 - 若不需要换行,可将
"\n"替换为", "等分隔符,实现同一单元格内逗号分隔展示。 - 建议提前通过
with(['tickets', 'emd', 'invoices'])预加载关联数据,避免N+1查询问题,提升导出效率。
内容的提问来源于stack exchange,提问作者Heshanmax
相关产品推荐
相关产品推荐

