Getting last element of the Collection

The last method call is not working because namespace Illuminate\Database\Eloquent; does not have any last() method. The right way to do this is to do something like this:

$last_saved_snapshot = EnginePerformanceTestSnapshot::where('engine_performance_test_id', $id)
                                                      ->orderBy('id', 'desc')
                                                      ->first();

Other wise, you need to get all items, since last() is part of the collection class so it expect a collection or array to be able to work and not a query builder, but it makes no sense to fetch all data just for one.


Try something like that:

$last_saved_snapshot = \EnginePerformanceTestSnapshot::where('engine_performance_test_id', $id)
    ->get()
    ->last();