Want to ask why my data from the database (MySQL) doesn’t all appear on the website from the grades
table join with the grade_details
table, my database is attached:
Screenshot my Database in MySQL
So this data is from the join grades
table with join grade_details
taken from the id_grade
data like the table above.
As given by the blue contact in the Quiz table, only Student31 values appear, while the other Quiz values are at 0, but the student names Servira and Student4 already have values, namely 90 and 80, as in the grade_details
table in the database. But to avoid confusion, the Servira and Student4 data don’t show up with a value of only 0. Even though I have used a join with another table, the other quiz scores cannot be read, only 1 student readable value data.
Code:
Model Grade Detail:
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');
}
}
Mode Grade:
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'));
}
Blade:
<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>
How do you make the Servira and Student4 column name fields filled with grades on quizzes on the web as well as in the grade_details
database table?
2
Answers
in your model Grade you should specify the details relationship as a HasMany relation not a BelongsTo relation so your model would be :
@AlirezaSalehi is correct. It is indeed a
hasMany
relationship. The correctdetails
relationship in yourGrade
is indeed:In the
GradeDetail
model, your primary key isid_grade_detail
(notid
), which is fine, so your relationships should be as following:Where
id_student
is the primary key of theUser
model, which I assume by the code you have given (out of the box, the primary key of theUser
model isid
, did you change that?)Check the documentation at https://laravel.com/docs/9.x/eloquent-relationships#one-to-many
In my experience, it’s good practice to follow the Laravel conventions, explained on the same page. Unless you have no other choice, f.ex. if the DB is already in existence. If it’s a fresh project, consider to follow the conventions. It will safe you debugging time later on. (speaking of personal experience)