Laravel:BadMethodCallException方法[find]不存在

Tar*_*fih 5 php laravel laravel-4

当尝试使用模型对象User从数据库中提取某些值时,我收到以下错误: BadMethodCallException Method [find] does not exist

这是我的文件:模型用户

<?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;
    }

}
Run Code Online (Sandbox Code Playgroud)

控制器用户:

<?php

class user extends BaseController {


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

}
Run Code Online (Sandbox Code Playgroud)

查看users/index.php

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>first page</title>

</head>
<body>
    <?php echo 'Hello'; ?>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

和routes.php:

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

谢谢您的帮助

Ant*_*iro 16

你不应该得到这个错误,因为你似乎在使用Eloquent并且有一个find()方法.

但是你得到了这个错误,有一些可能的原因:

1)User::你正在调用的是你在这里显示的不一样,检查你的控制器中的执行:

$reflector = new ReflectionClass("User");
$fn = $reflector->getFileName();
dd($fn);
Run Code Online (Sandbox Code Playgroud)

它必须向您显示您班级的完整路径.

2)自动加载有问题,你可以运行:

composer dumpautoload
Run Code Online (Sandbox Code Playgroud)

试图解决它.

3)Laravel源代码有问题,你可以删除Laravel代码:

rm -rf vendor/laravel
Run Code Online (Sandbox Code Playgroud)

然后重新安装

composer update
Run Code Online (Sandbox Code Playgroud)