我正在參加考試/測驗應用程序,它爲用戶創建測試/測驗,現在我必須創建一組包含數據的電子表格,例如給定考試中的現有學生,成績圖表等。生成具有多對多關係的PHPExcel Laravel Excel
Bu迄今爲止我所創建的所有用戶都使用` - > fromModel'創建表單,但如果使用任何關係和/或約束,我會得到一個空表。
我有這個型號:
<?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');
}
... 用戶模型(略) 類用戶擴展可驗證 { ... //關係 公共職能的答案(){ 回報 $這個 - >的hasMany(答::類); }
public function exams()
{
return $this->belongsToMany('EMMA5\Exam')->withPivot('active', 'started_at', 'ended_at', 'location_id');
}
...
,我試圖創建與用戶對於給定的考試片: (這是從我的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');
}
而且我得到一張空白紙。 我已經嘗試過->fromArray()
和->fromModel()
。 將不勝感激任何輸入。