Solution:
FinalyearSubject::where('subject_group', 'E1')
->orderBy('student_number', 'DESC')
->limit(2)
->get()
Explanation:
- Add filters
- Order them by student_number descending
- Take the top 2
get()
the result
In your example, the moment you are doing FinalyearSubject::get()
, the query is already done. get()
returns a Collection
object (more like an enriched array). Everything you chain afterwards is calculated using Laravel’s Collection utilities. ->get()
usually should be the last thing in your call so that you can do as much work in SQL as possible.