Model Class
findFirst()
Signature
Section titled “Signature”findFirst() — returns any
Available in: model
Category: Read Functions
Description
Section titled “Description”The findFirst() function fetches the first record from the database table mapped to the model, ordered by the primary key value by default. You can customize the ordering by passing a property name through the property argument, which is also aliased as properties. This makes it useful when you want the “first” record based on a specific field (e.g., earliest created date, alphabetically first name, lowest price, etc.). The result is returned as a model object.
Parameters
Section titled “Parameters”| Name | Type | Required | Default | Description |
|---|---|---|---|---|
property | string | no | [runtime expression] | Name of the property to order by. This argument is also aliased as properties. |
$sort | string | no | ASC |
Examples
Section titled “Examples”1. Get the first record by primary key (default behavior):
firstUser = model("user").findFirst();
Fetches the user with the lowest primary key value.
2. Get the first record alphabetically by name:
firstAuthor = model("author").findFirst(property="lastName");
Fetches the author with the alphabetically first last name.
3. Get the earliest created record (using a timestamp column):
firstArticle = model("article").findFirst(property="createdAt");
Fetches the oldest article based on creation date.
4. Get the cheapest product:
cheapestProduct = model("product").findFirst(property="price");
Fetches the product with the lowest price.
5. Use alias properties instead of property:
firstComment = model("comment").findFirst(properties="createdAt");
Works the same as property — useful when you prefer the plural alias.