Skip to main content

firstOrFail

The firstOrFail method returns the first record found in the given table. If no records exist, a RecordNotFound exception is raised.

const user = await User.firstOrFail()
// User{id=1}

select "users".* from "users"
order by "users"."id" asc nulls first
limit 1

Chaining

Similar to other execution methods, firstOrFail can be chained with other chainable query methods, such as order.

await User.order({ email: 'asc' }).firstOrFail()

select "users".* from "users"
order by "users"."email" asc nulls first
limit 1
tip

If firstOrFail is called within a request cycle and the RecordNotFound exception is not handled manually, Psychic will automatically throw a 404.