Laravel Eloquent Aggregates (Max) return 500 server error

Solution:

This code:

$lastRecord_sync = \App\TableModel::all()->max('record_id');

Is telling laravel to retrieve all records in the table as a collection, then get the max record_id in that collection (see: Collections: max), presumably by looping over the entire collection which can be extremely slow if you have many records.

The second snippet:

$lastRecord_sync = DB::table('tms_door_record_raw')->max('record_id');

Is telling laravel to essentally execute this query (see: Query Builder: Aggregates):

SELECT MAX('record_id') FROM table;

MySQL is much more efficient at finding the max record than PHP is looping through each record in the collection and finding the max record_id there.