Skip to content

Model Class

findLastOne()

findLastOne() — returns any

Available in: model Category: Read Functions

The findLastOne() function fetches the last record from the database table mapped to the model, ordered by the primary key value by default. You can override this ordering by passing a property name through the property argument (also aliased as properties). This is useful when you want to retrieve the “last” record based on something other than the primary key, such as the most recently created entry, the highest price, or the latest updated timestamp. The result is returned as a model object. This function was formerly known as findLast.

NameTypeRequiredDefaultDescription
propertystringnoName of the property to order by. This argument is also aliased as properties.
1. Get the last record by primary key (default behavior):

lastUser = model("user").findLastOne();

Fetches the user with the highest primary key value.

2. Get the last record alphabetically by name:

lastAuthor = model("author").findLastOne(property="lastName");

Fetches the author with the alphabetically last last name.

3. Get the most recently created record:

lastArticle = model("article").findLastOne(property="createdAt");

Fetches the article with the latest creation date.

4. Get the most expensive product:

priciestProduct = model("product").findLastOne(property="price");

Fetches the product with the highest price.

5. Use alias properties instead of property:

lastComment = model("comment").findLastOne(properties="createdAt");

Works the same as property — useful when you prefer the plural alias.