For example, I have five tables (School, Student, Class, Session, and Enrollment) the Enrollment Table store the Primary Key of other tables, Now I like to count in Session wise that how Many students have enrolled in Session 2019-2020 and display in the dashboard.
public function index()
{
$schools=School::all();
$students=Student::all();
$sessions=Session::all();
$enrollements=Enrollement::all();
return view('dashboard',compact('schools','students','enrollements','sessions'));
}
- when I write {{$sessions->latest()}} it show the following error """Method Illuminate\Database\Eloquent\Collection::latest does not exist""
- and how to pass session year (String) to enrollement to count?
could anyone suggest the best method to solve the following problem?
For collections , use
last()method ($sessions->last()) https://laravel.com/docs/7.x/collections#method-lastI don't know how your table is structured, but you need to group by year and then compare
Then in
$enrollementsByYearyou will have a collection where you can compare the year of the session and mount your table. Changeyearwith the actual column name.You can easily compare with something like: