I will explain what i have tried,
My tables are just two for the mark
MainMark with columns
id class_id section_id exam_id
1 1 1 1
2 1 1 2
SubMark with columns
id mainmark_id student_id subject_id mark
1 1 1 1 80
2 1 2 1 90
3 1 3 1 50
4 1 4 1 60
in the above table for a single subject that is (subject: english) i have entered mark for 4 students, the exam name, class section are refered from the mainmark table. how to fetch this as my expected output below
class: V STD Section: A Exam: Quarterly Exam
StudentName English Maths Science
Bazith 80 40 60
Abdul 60 70 80
Imran 69 75 85
Like above i need the output,
what i tried in coding is my controller
$students = StudentMainMark::where(function ($query) use ($request) {
if (!empty($request->class_id)) {
$query->where('class_id', $request->class_id);
}
if (!empty($request->section_id)) {
$query->where('section_id', $request->section_id);
}
if (!empty($request->exam_id)) {
$query->where('exam_id', $request->exam_id);
}
})->with('studentsubmark')->get();
In the above code iam fetching records based from mainmark table with class and section and exam id. with its sub mark.
when i dd($students);
Collection {#546 ▼
#items: array:1 [▼
0 => StudentMainMark {#545 ▼
#connection: "mysql"
#table: "student_main_marks"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:10 [▼
"id" => 34
"acc_year_id" => 1
"exam_date" => "2019-12-12"
"class_id" => 6
"section_id" => 16
"exam_id" => 2
"exam_remarks" => ""
"exam_status" => "active"
"created_at" => "2019-12-12 07:26:04"
"updated_at" => "2019-12-12 07:26:04"
]
#original: array:10 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▼
"studentsubmark" => Collection {#576 ▼
#items: array:4 [▼
0 => StudentSubMark {#580 ▼
#fillable: array:7 [▶]
#connection: "mysql"
#table: "student_sub_marks"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:10 [▼
"id" => 35
"acc_year_id" => 1
"main_mark_id" => 34
"student_id" => 10
"subject_id" => 12
"mark" => 57.0
"student_remarks" => null
"status" => "active"
"created_at" => "2019-12-12 07:26:04"
"updated_at" => "2019-12-12 07:26:04"
]
#original: array:10 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
1 => StudentSubMark {#581 ▶}
2 => StudentSubMark {#582 ▶}
3 => StudentSubMark {#583 ▶}
]
}
]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [▶]
}
]
}
in my view. blade file
<table class="table">
<thead>
<tr class="unread bggrn">
<th>S.No</th>
<th>Name</th>
@foreach($students->studentsubmark as $sub)
<th>{{ $sub->subject->sub_name }}</th>
@endforeach
</tr>
</thead>
<tbody class="capital">
@foreach($students->studentsubmark as $sub)
<tr class="profile-table">
<td> {{ $loop->iteration }} </td>
<td> <a href="">{{ $sub->user->user_name }} </a> </td>
<td> {{ $sub->mark }} </td>
<td>{{ $sub->student_remarks }}</td>
</tr>
@endforeach
</tbody>
</table>
Expected Output:
StudentName English Maths Science
Bazith 80 40 60
Abdul 60 70 80
Imran 69 75 85
in the above view iam trying to make a output like mentioned. but the subjects and name are repeated. why?
and this are my models
Main Mark Model
class StudentMainMark extends Model
{
public function studentsubmark()
{
return $this->hasmany('App\StudentSubMark', 'main_mark_id');
}
}
SubMark Model
public function studentmainmark()
{
return $this->belongsTo('App\StudentMainMark', 'main_mark_id');
}
public function user()
{
return $this->belongsTo('App\User', 'student_id');
}
public function subject()
{
return $this->belongsTo('App\SubjectInfo', 'subject_id');
}
But i face a problem is the names and subjects are repeated in the view. blade as for loop is mistake or something else i dont know