Hi guys... I want to display all the users with roles = learners, grade equals to specific Grade and subjects equals to a specific subject... I have hardcoded it and works OK but How do I do it dynamically?
ClassController
public function index()
{
return view('classes.index');
}
index.blade.php
@if(count(Auth::user()->grades) > 0)
@foreach(Auth::user()->grades as $grade)
<div class="col-md-2">
<div class="card shadow mb-4">
<div class="card-body">
<form method="POST" action="{{ route('class.show') }}">
@csrf
<h5 class="font-weight-bold">{{$grade->name}}</h5>
<input type="hidden" name="grade" value="{{$grade->name}}"/>
<hr>
<h6 class="font-weight-bold">Subjects:</h6>
<ul>
@foreach(Auth::user()->subjects as $subject)
<li>{{ $subject->name }}</li>
<input type="hidden" name="subjects[]" value="{{$subject->name}}"/>
@endforeach
</ul>
<hr>
<div class="row">
<div class="col-md-12">
<button type="submit" class="btn btn-primary"><i class="fas fa-graduation-cap fa-fw mr-2"></i>View Learners</button>
</div>
</div>
</form>
</div>
</div>
</div>
@endforeach
@else
<h3 class="col-md-12 offset-md-5">No Grades to display</h3>
@endif
Here is where I have hard coded the values (Grade 12, Mathematics) and it works, but I want to do it dynamically...
ClassController
public function show(Request $request)
{
$learners = User::whereHas('roles', function($query){
$query->where('name', 'learner');
})->whereHas('grades', function($query){
$query->where('name', 'Grade 12');
})->whereHas('subjects', function($query){
$query->where('name', 'Mathematics');
})->get();
}
return view('classes.show', compact('learners'));
}