Home > Back-end >  Why isn't all the data showing up in Laravel?
Why isn't all the data showing up in Laravel?

Time:12-29

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.

Screenshot in web

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 >
    <thead >
    <tr>
        <th scope="col" >
            No
        </th>
        <th scope="col" >
            NISN
        </th>
        <th scope="col" >
            Name Student
        </th>
        <th scope="col" >
            Min Score
        </th>
        <th scope="col" >
            Quiz
        </th>
        <th scope="col" >
            Assignment
        </th>
        <th scope="col" >
            Daily Tests
        </th>
        <th scope="col" >
            Min Exam
        </th>
        <th scope="col" >
            Final Exam
        </th>
        <th scope="col" >
            Total
        </th>
    </tr>
    </thead>
    <tbody>
    @php $no = 1; @endphp
    @forelse($subject as $data)
        <tr >
            <th scope="row" >
                {{$no  }}
            </th>
            <th scope="row" >
                {{$data->username}}
            </th>
            <th scope="row" >
                {{$data->name}}
            </th>
            <th scope="row" >
                {{$data->min_score}}
            </th>
            <th scope="row" >
                @if(!empty($data->details))
                    {{ (!empty( $data->id == $data->details->id_student )) ? $data->details->quiz: 0}}
                @else
                    0
                @endif
            </th>
            <th scope="row" >
                @if(!empty($data->details))
                    {{$data->details->assignment}}
                @else
                    0
                @endif
            </th>
            <th scope="row" >
                @if(!empty($data->details))
                    {{$data->details->d_t}}
                @else
                    0
                @endif
            </th>
            <th scope="row" >
                @if(!empty($data->details))
                    {{$data->details->min_text}}
                @else
                    0
                @endif
            </th>
            <th scope="row" >
                @if(!empty($data->details))
                    {{$data->details->final_text}}
                @else
                    0
                @endif
            </th>
            <th scope="row" >
                @if(!empty($data->details))
                    {{$data->details->total}}
                @else
                    0
                @endif
            </th>
        </tr>
    @empty
        <tr colspan = "10" >
            <td >
                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?

CodePudding user response:

in your model Grade you should specify the details relationship as a HasMany relation not a BelongsTo relation so your model would be :

public function details(){
    $this->hasMany(GradeDetail::class, 'id_grade','id_grade');  
}

CodePudding user response:

Your expected results depends upon subjects, class_infos, class_details, users table and where condition so if one of them is empty or not matching your condition then you will not get your result. Try using left join without where condition for debugging. For more debugging try following method .

    $subject = Grade::join('subjects', 'grades.id_subject', '=', 'subjects.id_sub')
    ->where('subjects.id_teacher', '=', Auth::user()->id)
    ->where('grades.id_grade', '=', $id)
    ->get();

just check above results and debug if that query returning your expecting result then add other join clause one by one

  • Related