Laravel select last row from table


Laravel select last row from table : You can get the last row from table by sorting the data in descending order and then get the first row which will give the last row from the table. Here in this example we are going to explain how to get the last row from table.


Laravel select last row from table

You can select the last row from table as below-

Laravel select last row from table:

DB::table('MyTable')->orderBy('ID', 'desc')->first();

The above example will give you the last row from the table Mytable.

If you are using the Eloquent you can select the last record as below-

Laravel select last row from table:

User::orderBy('ID', 'desc')->first();

The above example will fist sort by column ID(auto increment) in descending order and then it will select the first result which will give you the last record from table.


Advertisements

Add Comment

📖 Read More