[英]Why isn't all the data showing up in Laravel?
想问一下为什么我的数据库(MySQL)的数据没有全部出现在grades
表join和grade_details
表的网站上,附上我的数据库:
所以这个数据来自连接grades
表,连接等级_详细信息取自grade_details
数据, id_grade
表。
正如 Quiz 表中蓝色联系人给出的那样,只有Student31值出现,而其他 Quiz 值都为0 ,但是学生名称Servira和Student4已经有值,即90和80 ,如数据库中的grade_details
表中所示。 但是为了避免混淆,Servira 和 Student4 数据并没有显示只有 0 的值。即使我使用了与另一个表的连接,也无法读取其他测验分数,只有 1 个学生可读值数据。
代码:
Model 等级详情:
class GradeDetail extends Model
{
use HasFactory;
protected $primaryKey = 'id_grade_detail';
protected $table = 'grade_details';
protected $fillable = [
'id_grade_detail',
'id_grade',
'id_student',
'quiz',
'assignment',
'd_t',
'min_text',
'final_text',
'total',
];
public function grades(){
return $this->belongsTo(Grade::class, 'id_grade','id_grade');
}
public function students(){
return $this->belongsTo(User::class, 'id_student','id');
}
}
模式等级:
class Grade extends Model
{
use HasFactory;
protected $primaryKey = 'id_grade';
protected $table = 'grades';
protected $fillable = [
'id_grade',
'id_subject',
'id_academic_year',
'id_semester',
'min_score',
'done',
];
public function details(){
return $this->belongsTo(GradeDetail::class, 'id_grade','id_grade');
}
}
Controller:
public function ViewGrade($id){
$subject = Grade::join('subjects', 'grades.id_subject', '=', 'subjects.id_sub')
->join('class_infos', 'subjects.id_class', '=', 'class_infos.id')
->join('class_details', 'class_infos.id', '=', 'class_details.id_class')
->join('users', 'class_details.id_user', '=', 'users.id')
->where('subjects.id_teacher', '=', Auth::user()->id)
->where('grades.id_grade', '=', $id)
->get();
return view('teacher.grade.view_grade', compact('subject'));
}
刀:
<table class="w-full text-sm text-left text-gray-500 dark:text-gray-400">
<thead class="text-xs text-white uppercase bg-[#464867] dark:bg-[#464867]">
<tr>
<th scope="col" class="py-3 px-6">
No
</th>
<th scope="col" class="py-3 px-6">
NISN
</th>
<th scope="col" class="py-3 px-6">
Name Student
</th>
<th scope="col" class="py-3 px-6">
Min Score
</th>
<th scope="col" class="py-3 px-6">
Quiz
</th>
<th scope="col" class="py-3 px-6">
Assignment
</th>
<th scope="col" class="py-3 px-6">
Daily Tests
</th>
<th scope="col" class="py-3 px-6">
Min Exam
</th>
<th scope="col" class="py-3 px-6">
Final Exam
</th>
<th scope="col" class="py-3 px-6">
Total
</th>
</tr>
</thead>
<tbody>
@php $no = 1; @endphp
@forelse($subject as $data)
<tr class="bg-white border-b dark:bg-gray-900 dark:border-gray-700">
<th scope="row" class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
{{$no++}}
</th>
<th scope="row" class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
{{$data->username}}
</th>
<th scope="row" class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
{{$data->name}}
</th>
<th scope="row" class="py-4 px-6 font-medium text-red-700 whitespace-nowrap dark:text-white">
{{$data->min_score}}
</th>
<th scope="row" class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
@if(!empty($data->details))
{{ (!empty( $data->id == $data->details->id_student )) ? $data->details->quiz: 0}}
@else
0
@endif
</th>
<th scope="row" class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
@if(!empty($data->details))
{{$data->details->assignment}}
@else
0
@endif
</th>
<th scope="row" class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
@if(!empty($data->details))
{{$data->details->d_t}}
@else
0
@endif
</th>
<th scope="row" class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
@if(!empty($data->details))
{{$data->details->min_text}}
@else
0
@endif
</th>
<th scope="row" class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
@if(!empty($data->details))
{{$data->details->final_text}}
@else
0
@endif
</th>
<th scope="row" class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
@if(!empty($data->details))
{{$data->details->total}}
@else
0
@endif
</th>
</tr>
@empty
<tr colspan = "10" class="bg-white border-b dark:bg-gray-900 dark:border-gray-700">
<td class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
No Data
</td>
</tr>
@endforelse
</tbody>
</table>
如何在 web 以及grade_details
数据库表中的Servira和Student4列名字段中填入测验的成绩?
在您的 model 成绩中,您应该将详细信息关系指定为HasMany关系而不是BelongsTo关系,因此您的 model 将是:
public function details(){
$this->hasMany(GradeDetail::class, 'id_grade','id_grade');
}
您的预期结果取决于主题、class_infos、class_details、用户表和 where 条件,因此如果其中一个为空或不符合您的条件,那么您将不会得到结果。 尝试使用不带 where 条件的 left join 进行调试。 如需更多调试,请尝试以下方法。
$subject = Grade::join('subjects', 'grades.id_subject', '=', 'subjects.id_sub')
->where('subjects.id_teacher', '=', Auth::user()->id)
->where('grades.id_grade', '=', $id)
->get();
只需检查以上结果并调试该查询是否返回您的预期结果然后一一添加其他连接子句
@AlirezaSalehi 是正确的。 它确实是一个hasMany
关系。 您Grade
中正确的details
关系确实是:
public function details(){
$this->hasMany(GradeDetail::class, 'id_grade','id_grade');
}
在GradeDetail
model 中,您的主键是id_grade_detail
(不是id
),这很好,因此您的关系应如下所示:
public function grades(){
return $this->belongsTo(Grade::class, 'id_grade','id_grade_detail');
}
public function students(){
return $this->belongsTo(User::class, 'id_student','id_grade_detail');
}
其中id_student
是User
model 的主键,我根据您提供的代码假设(开箱即用, User
model 的主键是id
,您更改了吗?)
查看文档https://laravel.com/docs/9.x/eloquent-relationships#one-to-many
根据我的经验,遵循 Laravel 约定是一种很好的做法,在同一页上进行了解释。 除非你别无选择,f.ex。 如果数据库已经存在。 如果是新项目,考虑遵循约定。 以后的调试时间会很安全。 (说到个人经历)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.