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

Creed06's avatar

findOrFail

Hello what's happen to findOrFail function in Eloquent?

they removed it : https://github.com/laravel/framework/commit/2f59c2c5459530e9a0be8c40ac536c06f3e99973

now since laravel is 5.0.2 how to use that function

thank you

0 likes
24 replies
Creed06's avatar

Hello thank you but it was avaible yesterday before the update. And now with 5.0.2 it can't be found and when i check the file it's no more here as you can see on git it was remove like 2 weeks ago and that fix got implemented into 5.0.2

pmall's avatar

So how are we supposed to find or fail something now ?

Creed06's avatar

Well i don't know they removed more function ><

Creed06's avatar

I just roll back the framework to 5.0.0 for be sure that i didn't made a mistake but yeah all is back

so the issue is on the new release 5.0.2 they removed many things >< so now i'm lost

bestmomo's avatar

Anyone knows why these methods are unnecessary ?

pmall's avatar

Maybe now find === findOrFail ?

It would be cool if now Eloquent gets improved a bit.

bestmomo's avatar

Yes I confirm, I just updated with 5.0.2 and all is working fine ^^

JarekTkaczyk's avatar
Level 53

@Creed06

It's still there, only it doesn't make sense at all to have it static on the Model, since it is a method on the Eloquent\Builder class. So feel free to use it and don't worry at all.

Have in mind that every static call to the model is either served by the Model static methods, or by the Eloquent\Builder on the instantiated object, or if no such methods exists, on the Query\Builder. Only then will you get BadMethodCallException, if there is no methods defined on the latter.

1 like
Creed06's avatar

Well dunno why first time it doesnt work for me but alter roolback framework to 5.0..0 then update again to 5.0.2 it's work >< weird bug

anyway thank you for your reply guys :)

pmall's avatar

Didn't know I could throw an array of ids in the find or findOrFail method. Learned something :D

3 likes
pmall's avatar

@JarekTkaczyk Look at the link jeffrey posted, findOrFail use find and check if there is as many rows as ids.

pmall's avatar

This whole time i used whereIn with a check if there is zero items, haha

bestmomo's avatar

So I looked in API, it's the signature :

Model|Collection findOrFail(mixed $id, array $columns = array('*'))
michaeldyrynda's avatar

You didn't really think Taylor would make such a breaking change in a patch release, did you? Just removing a bit of duplication :)

AlanRezende's avatar

I was using a trait to catch the fail exception (ModelNotFoundException) and retrow then as TenantModelNotFoundException. In the trait i had a method findOrFail that replaced the parent method that was on the model.

/**
     * Override the default findOrFail method so that we can rethrow a more useful exception.
     * Otherwise it can be very confusing why queries don't work because of tenant scoping issues.
     *
     * @param       $id
     * @param array $columns
     *
     * @throws TenantModelNotFoundException
     */
    public static function findOrFail($id, $columns = ['*'])
    {
        try {
            return parent::findOrFail($id, $columns);
        } catch (ModelNotFoundException $e) {
            throw with(new TenantModelNotFoundException())->setModel(get_called_class());
        }
    }

how can i achieve the same result (parent::findOrFail($id, $columns);) now that findOrFail is not available in the model?

JarekTkaczyk's avatar

@AlanRezende

// replace
return parent::findOrFail($id, $columns);
// with
return call_user_func_array([static::query(), 'findOrFail'], [$id, $a]);

Please or to participate in this conversation.