Search code examples
laravellaravel-5eloquent

Find max value of a column in laravel


The problem started because I have a table (Clientes), in which the primary key is not auto-incremental. I want to select the max value stored in a column database.

Like this select, but with eloquent ORM (Laravel):

SELECT MAX(Id) FROM Clientes

How can I do this?

I tried:

Cliente::with('id')->max(id);
Cliente::select('id')->max(id);

I prefer not to make a simple raw SELECT MAX(ID) FROM Clientes

I cannot make it.

Thanks all!


Solution

  • The correct syntax is:

    Cliente::max('id')
    

    https://laravel.com/docs/5.5/queries#aggregates