Mysql - Laravel : Get Last Row of table without fetching all data -
i using laravel , need retrieve last row of table. i'm using code below apparently getting whole table , looping last element . become issue if table huge.
$result = table::orderby('created_at', 'desc')->first();
is there better , more efficient way last row : eloquent or sql query.
thanks
your current laravel query 1 way find recent row of database. mentioned @vladatr corresponds following raw mysql query:
select * yourtable order created_at desc limit 1
to answer question, 1 way speed query @ mysql level add index on created_at
column. index, if used optimizer, should speed sorting operation needed find recent record.
Comments
Post a Comment