[FIXED] Laravel : BadMethodCallException Method [find] does not exist

Issue

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

Solution

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

Answered By – Antonio Carlos Ribeiro

Answer Checked By – Gilberto Lyons (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published