Reputation: 61
I am trying to get a report using laravel eloquent but have stumbled on an issue!
Report Goal: get all students the task is assigned too and the progress they have made.
I have a controller setup:
public function show($task) {
return new TaskStudentProgressStats(Task::findOrFail($task));
}
works fine, then my resources work flow is like the following:
Task(Groups(Students(Progress())))
I get all the data but the progress is not related to the Task, How can I maybe pass the task id to the progress resource? Or can I call some data from a parent resource?
Student Progress Relationship is the following:
public function studentProgress() {
return $this->hasMany(ProgressStudentTaskUser::class, 'id', 'student_id');
}
Upvotes: 0
Views: 1522
Reputation: 192
If the Task
model hasMany students
and students hasMany prgress
then try this code:
public function show($task) {
return Task::find($task)->students()->with('progress')->get();
}
this should return all students of this task with there progress
note that this will return all the progress of each student if you want to customize the relation then use:
public function show($task) {
return Task::find($task)->students()->with([ 'progress' => function($q){
$q->where('field', 'value');
} ])->get();
}
to call it in a view you will use:
@foreach ($students as $student)
<div>{{ $student->name }} - {{ $student->progress->name }}</div>
@endforeach
Upvotes: 0