php - 如何使用 Laravel excel 3.1 将一对多关系映射为一行

标签 php laravel-5.7 laravel-excel

我已关注文档maatwebsite版本 3.1,当导出为 Excel 时,我正在努力将所有问题选择映射到带有问题的行中。 这是我在 App\Exports\QuestionExcel.php 中所做的操作:

<?php

namespace App\Exports;

use App\Question as Question;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithMapping;

class QuestionExcel implements FromCollection, WithHeadings, WithMapping
{
    use Exportable;
    /*
        Question $question
    */
    private $i = 1;

    public function collection()
    {
        return Question::with('level','questionType','questionChoices')->get();
    }

    public function map($question): array
    {
        // $question 

        return [
            $this->i++,
            $question->description,
            $question->questionType->name,
            $question->level->name,
            $question->duration,
            $question->questionChoices->map(function ($choice){
                              return $choice->label;
                           }),
        ];
    }


    public function headings(): array
    {
        return [
            'No',
            'Question',
            'Type',
            'Level',
            'Duration (s)',
        ];
    }
}

在App\Http\Controllers\ExcelController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Maatwebsite\Excel\Excel;
use App\Exports\QuestionExcel;
use App\Level as Level;

class ExcelController extends Controller
{
    private $excel;

    public function __construct(Excel $excel)
    {
        $this->excel = $excel;
    }

    public function export_questions($level_id){
       $level = Level::find($level_id);
       $level_name = str_replace(' ', '_', $level->name);
       return (new QuestionExcel)->download($level_name.'_question_exports.xlsx');
    }
}

结果 The result of excel

请有人帮助我得到这样的结果吗? Expected result

最佳答案

最后,我通过使用 From View 找到了解决方案

在应用\Exports\QuestionExcel.php

    <?php

    namespace App\Exports;

    use App\Question as Question;
    use Illuminate\Contracts\View\View;
    use Maatwebsite\Excel\Concerns\FromView;
    use Maatwebsite\Excel\Concerns\ShouldAutoSize;
    // for applying style sheet
    use Maatwebsite\Excel\Concerns\WithEvents;
    use Maatwebsite\Excel\Events\AfterSheet;
    Use \Maatwebsite\Excel\Sheet;

    class QuestionExcel implements FromView, WithEvents, ShouldAutoSize
    {
        // use Exportable;
        /*
            Question $question
        */
        public function __construct($level_id)
        {
            $this->level_id = $level_id;
        }


        public function view(): View
        {
            $questions = Question::where('level_id',$this->level_id)->with(['level','questionType','questionChoices'])->withCount('questionChoices');
            return view('excels.question_exports', [
                'questions' => $questions->get(),
                'option_column' =>  $questions->orderBy('question_choices_count','DESC')->first()
                // option_column, will return the largest number of question choices so we can create dynamic header in table
            ]);
    }

}

在views\excels.question_exports.blade.php中

<html>
    <head>
    </head>
    <body>
        <table>
            <tr>
                <th rowspan="2">No</th>
                <th rowspan="2">Question</th>
                <th rowspan="2">Type</th>
                <th rowspan="2">Level</th>
                <th rowspan="2">Duration (s)</th>
                @for($i=1; $i<= $option_column->question_choices_count; $i++)
                    <th colspan="3">Option {{ $i}}</th>
                @endfor
            </tr>
            <tr>
                @for($i=1; $i<= $option_column->question_choices_count; $i++)
                    <th>Answer</th>
                    <th>Set correct</th>
                    <th>Explanations</th>
                @endfor 
            </tr>
            @foreach($questions as $index => $question)
            <tr>
                <td>{{ ($index+1) }}</td>
                <td>{{ $question->description }}</td>
                <td>{{ $question->questionType->name }}</td>
                <td>{{ $question->level->name }}</td>
                <td>{{ $question->duration }}</td>
                @foreach($question->questionChoices as $choice)
                    <td>{{ $choice->label }}</td>
                    <td>{{ $choice->is_correct?1:0 }}</td>
                    <td>{{ $choice->choice_explaination }}</td>
                @endforeach
            </tr>
            @endforeach
        </table>
    </body>
</html>

在App\Http\Controllers\ExcelController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Maatwebsite\Excel\Excel;
use App\Exports\QuestionExcel;
use App\Level as Level;

class ExcelController extends Controller
{
    private $excel;

    public function __construct(Excel $excel)
    {
        $this->excel = $excel;
    }

    public function export_questions($level_id){
        $level_name = Level::find($level_id)->name;
        return \Excel::download(new QuestionExcel($level_id), str_replace(" ","_",$level_name)."_question_exports.xlsx");
    }
}

Result of excel

关于php - 如何使用 Laravel excel 3.1 将一对多关系映射为一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53605445/

相关文章:

php - Laravel Excel 如何将 View 加载到特定行或单元格?

php - 没有 "finally"的编程

javascript - 无法使用带有 php 的 jquery ajax 将数据添加到数据库中

laravel - Stream_socket_sendto() : Connection refused. Laravel 5.7 转储服务器

Angular "Http failure during parsing for http://localhost:8000/excel"

Laravel excel maatwebsite 导入,Excel 单元格中的日期列返回为未知格式数字。怎么解决这个问题呢?

javascript - 使用内联文本编辑更新数据库

php mysql Paypal IPN mysql查询有问题

php - 在 Windows 上使用 Laravel 5.7 出现 500 服务器错误

laravel - 没有计划的命令准备好运行 |仅在 $schedule