Laravel : BadMethodCallException Method [find] does not exist

73,195

Solution 1

You should not get this error, because it seems like you are using Eloquent and there is a find() method on it.

But you are getting this error and there are some possible reasons:

1) The User:: you are calling is not the same you are showing here, to check execute in your controller:

$reflector = new ReflectionClass("User");
$fn = $reflector->getFileName();
dd($fn);

It must show you the full path of your class.

2) There's is something wrong with autoloading, you can run:

composer dumpautoload

To try to fix it.

3) There is something wrong with Laravel sources, you can delete Laravel code:

rm -rf vendor/laravel

and install it again

composer update

Solution 2

Sometimes this conflict is caused when another model of the same name exists in Laravel and most likely has a precedence over your model. Avoid "Key Words". For me, my problem was calling my model: Form and my db table (from a migration): forms.

Running the following cmd in my FormController:

$reflector = new ReflectionClass("Form");
$fn = $reflector->getFileName();
dd($fn);

showed me that the "real" Form was located in the Illuminate framework:

"/home/pkanane/laravel_appz/cpay/vendor/laravel/framework/src/Illuminate/Support/Facades/Form.php" 

So I just changed my model name to WebForm and my db table to web_forms

Share:
73,195
Tarik Mokafih
Author by

Tarik Mokafih

Updated on July 05, 2022

Comments

  • Tarik Mokafih
    Tarik Mokafih over 1 year

    when trying to extract some values from the database using the model object User I get the following error : BadMethodCallException Method [find] does not exist

    Here are my files : Model User

    <?php
    use Illuminate\Auth\UserInterface;
    use Illuminate\Auth\Reminders\RemindableInterface;
    class User extends Eloquent implements UserInterface, RemindableInterface {
        /**
         * The database table used by the model.
         *
         * @var string
         */
        protected $table = 'users';
        /**
         * The attributes excluded from the model's JSON form.
         *
         * @var array
         */
        protected $hidden = array('password');
            public function projects()
            {
                return $this->belongsToMany('Project');
            }
            public function trys()
            {
                return $this->hasMany('Try');
            }
        /**
         * Get the unique identifier for the user.
         *
         * @return mixed
         */
        public function getAuthIdentifier()
        {
            return $this->getKey();
        }
        /**
         * Get the password for the user.
         *
         * @return string
         */
        public function getAuthPassword()
        {
            return $this->password;
        }
        /**
         * Get the e-mail address where password reminders are sent.
         *
         * @return string
         */
        public function getReminderEmail()
        {
            return $this->email;
        }
    }
    

    Controller user :

    <?php
    class user extends BaseController {
        public function showWelcome($id)
        {
            $user1 = User::find($id);
            return View::make('users.index', array('user' => $user1)); //
        }
    }
    

    View users/index.php

    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>first page</title>
    </head>
    <body>
        <?php echo 'Hello'; ?>
    </body>
    </html>
    

    and the routes.php :

    <?php
    Route::get('user/{id}', 'user@showWelcome');
    Route::get('/', function()
    {
        return View::make('hello');
    });
    

    Thanks for the help

  • Tarik Mokafih
    Tarik Mokafih over 9 years
    I tried the 3 options none of them worked, with option 1 I got : Class \ConsultorioDigital\Models\VocalUser\Model does not exist
  • Antonio Carlos Ribeiro
    Antonio Carlos Ribeiro over 9 years
    Sorry that was my class name, I was doing a test with it here and forgot to edit. Change it to User, of course.
  • Tarik Mokafih
    Tarik Mokafih over 9 years
    It showed me a wrong file name : string(48) "/mylaravelapp/app/controllers/user.php"
  • Tarik Mokafih
    Tarik Mokafih over 9 years
    It seems there is a conflict between the model class User.php and the controller class user.php, I solved it by chaging the controller class name to UserController. Thank you Carlos
  • Kyle Burkett
    Kyle Burkett almost 7 years
    I am using a model named Form and a table named forms and encountered this exact same issue, I am changing my model and table names now. Crazyy