我正在开发一个考试/测验应用程序,它为用户创建测试/测验,现在我必须创建一组电子表格,其中包含给定考试中的当前学生、成绩表等数据。
到目前为止,我设法创建的只是一张所有用户都使用 `->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');
}
... 用户模型(简称) class User extends Authenticatable { ... //关系 public function answers() { return $this->hasMany(Answer::class); }
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()
and ->fromModel()
。将不胜感激任何输入。