简体   繁体   English

生成具有多对多关系的PHPExcel Laravel Excel

[英]Generate PHPExcel Laravel Excel with many to many relations

I am working on an exam/quiz app and it creates tests/quizzes for users and now I must create a set of spreadsheets that contain data such as the present students in a given exam, grades charts and so on. 我正在开发一个考试/测验应用程序,它为用户创建了测试/测验,现在我必须创建一组电子表格,其中包含诸如给定考试中的当前学生,成绩表等数据。

Bu so far all I managed to create is a sheet with ALL the users using `->fromModel' but if I use any relation and or constrain I get an empty sheet. 到目前为止,我设法创建的所有工作表都是使用`-> fromModel'的所有用户,但是如果我使用任何关系和/或约束,则会得到一个空工作表。

I have this models: 我有这个模型:

 <?php

namespace EMMA5;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Exam extends Model
{
    //

    protected $dates = ['created_at', 'updated_at', 'applicated_at'];

    protected $fillable = [
        'applicated_at',
        'duration',
        'board_id',
        'passing_grade',
        'annotation'
   ];

    public function board()
    {
        return $this->belongsTo('EMMA5\Board');
    }

    public function users()
    {
        return $this->belongsToMany('EMMA5\User')->withPivot('active', 'started_at', 'ended_at', 'seat', 'location_id');
    }

... User model (abbreviated) class User extends Authenticatable { ... //Relations public function answers() { return $this->hasMany(Answer::class); ...用户模型(缩写)类用户扩展Authenticateable {... //关系公共函数Answer(){return $ this-> hasMany(Answer :: class); } }

    public function exams()
    {
        return $this->belongsToMany('EMMA5\Exam')->withPivot('active', 'started_at', 'ended_at', 'location_id');
    }

... ...

And I am trying to create a sheet with the users for a given exam: (This is from my ExamController.php) 我正在尝试针对给定的考试与用户一起创建一张表格:(这来自我的ExamController.php)

/**
     * Returns a spreadsheet with only the students that were present
     *
     * @return PHPOffice
     */
    public function gradesSpreadshet(Exam $exam)
    {
            $grade = new Grade;
            $gradedStudents = $grade->allStudents($exam)->toArray();
            //dd(\EMMA5\Exam::find(195)->with('users')->get());
            //dd($exam->answers->answer);
            $data = $exam->users;
            return Excel::create("FinalGrades", function ($excel) use($data) {
                    //Create sheet to be able to return something to keep on testing

                    //Another sheet
                    $excel->sheet('Primera hoja', function ($sheet) use($data) {
                            $sheet->fromArray($data);
                    });
            })->export('xlsx');
    }

And I get an empty sheet. 我得到一张空白纸。 I already tried with ->fromArray() and ->fromModel() . 我已经尝试过->fromArray()->fromModel() Will appreciate any input. 将不胜感激任何输入。

Some time passed and nobody answered but I found a solution. 过了一段时间,没人回答,但我找到了解决方案。 I do not know if it will be helpful for someone else. 我不知道这是否对其他人有帮助。

The way I got the results couldn't be read by Excel Laravel. Excel Laravel无法读取我得到结果的方式。 So I created a helper function with a callback. 所以我创建了一个带有回调的辅助函数。

public static function collectionToArray(Array $collect = null) { return array_map(function ($array) use($collect) { foreach ($array as $key => $value) { $resultArray[$key] = $value; } return $resultArray; }, $collect); } public static function collectionToArray(Array $collect = null) { return array_map(function ($array) use($collect) { foreach ($array as $key => $value) { $resultArray[$key] = $value; } return $resultArray; }, $collect); } This returns a simplified version of the Collection that can be easily read by Excel Laravel. public static function collectionToArray(Array $collect = null) { return array_map(function ($array) use($collect) { foreach ($array as $key => $value) { $resultArray[$key] = $value; } return $resultArray; }, $collect); }这将返回Collection的简化版本,Excel Laravel可以轻松读取它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM