Viernes's avatar

findOrFail Not Working

As i use User::findOrFail(); it's gives me method findOrFail not found in usr model at the User Model :- i tried -> use Illuminate\Database\Eloquent\Builder; but no hope i see it in the documentation then it suppose still working..

1 like
16 replies
tykus's avatar

Does your User model have Illuminate\Database\Eloquent\Model in its hierarchy?

1 like
Viernes's avatar
namespace App;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function address()
    {
        return $this->hasMany('App\Address');
    }
}
staudenmeir's avatar

Do you have a use App\User; statement in your routes file?

Viernes's avatar

Yes

use App\Address;
use App\User;
use Illuminate\Support\Facades\Route;



Route::get('/', function () {
    return view('welcome');
});

Route::get('/insert',function(){

    $user = User::findOrFail(1);

    $address = new address(['name'=>'145 maadi']);

    $user->address()->save($address);

    return 'Done';

});
staudenmeir's avatar

Please post the exact error message and the stacktrace.

Viernes's avatar

I am New in laravel and in the first stages so i can't use Exception stack trace in the moment so am using Phpstorm and i got the error when i get my arrow to the findOrFail() it gives me this message -> " method 'findOrFail' not found in App\User Referenced Method is not found in the subject class ".

staudenmeir's avatar

PhpStorm shows this error in your code? Did you actually run the code by visiting the /insert page?

1 like
petrit's avatar

Have you tried on browser? The fact that method findOrFail is not working on Phpstorm it does not mean that it wont work in real.

1 like
Snapey's avatar

when i get my arrow to the findOrFail()

I cannot see you using an arrow, you used User::findOrFail Are you showing the right code?

Also in web.php use the full namespace rather than trying to import it.

Route::get('/insert',function(){

    $user = App\User::findOrFail(1);

    $address = new App\Address(['name'=>'145 maadi']);

    $user->address()->save($address);

    return 'Done';

});
Cronix's avatar
Cronix
Best Answer
Level 67

@snapey I think he means his IDE (phpstorm) reports the findOrFail method is not found, not that it won't work if executed.

@Viernes Laravel uses lots of magic behind the scenes that a lot of IDE's can't pick up on.

In phpstorm, make sure you have the laravel plugin installed and enabled. Also, this package will help phpstorm find a lot of the magic that the IDE can't figure out: https://github.com/barryvdh/laravel-ide-helper

4 likes
Snapey's avatar

@cronix

I think he means his IDE (phpstorm) reports the findOrFail method is not found, not that it won't work if executed.

I know. having Use statements inside web.php is probably confusing it since its not a class

Viernes's avatar

@Cronix Thanks man it's works now after the installation of the package you mentioned and your theory very true the code works fine even if it's show an error in (Phpstorm) as you said "Laravel uses lots of magic behind the scenes that a lot of IDE's can't pick up on.". Thanks Again (Y)

jesuscam's avatar

I know this is an old post but I want to provide the answer. Since this issue shows on PhpStorm you have to run the Generate Helper Code on the Laravel menu on PhpStorm. It will build the helper classes that the current project needs to ensure that we don't get these kind of errors.

After running this, the errors will automatically hide, in this case for the findOrFail.

1 like
eberte_sampaio's avatar

Hello guys, today I woke up wondering: "Why doesn’t PhpStorm recognize some model static functions?" I searched a bit on the internet and found out! You just need to download the Laravel Idea plugin (currently free), go to Languages & Frameworks > Laravel Idea > Eloquent, check the option "Implicit Eloquent method usages searcher (might use a lot of CPU resources)", and apply.

Please or to participate in this conversation.