Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

davy_yg's avatar
Level 27

findOrFail();

Hello,

I wonder what is the difference between findOrFail and find ?

$Product = Product:findOrFail($id);

Thanks in advance.

0 likes
5 replies
mehedi's avatar
mehedi
Best Answer
Level 2

When you use only find method it's return null when no data found. But and when you use findOrFail method it's throw an Exception when no data found.

3 likes
Snapey's avatar

a) You FIND it

b) You FIND it OR you FAIL (throw an exception)

2 likes
gorakhyadav's avatar

1- find($id) takes an id and returns a single model. If no matching model exist, it returns null.

2- findOrFail($id) takes an id and returns a single model. If no matching model exist, it throws an error1.

1 like
jlrdw's avatar

I am so sorry that I am confused here.

But it is right in the docs

Sometimes you may wish to throw an exception if a model is not found. This is particularly useful in routes or controllers. The findOrFail and firstOrFail methods will retrieve the first result of the query; however, if no result is found, a Illuminate\Database\Eloquent\ModelNotFoundException will be thrown:

Again sorry.

Please or to participate in this conversation.