php Laravel:BadMethodCallException 方法 [find] 不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22882347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:20:31  来源:igfitidea点击:

Laravel : BadMethodCallException Method [find] does not exist

phplaravellaravel-4

提问by Tarik Mokafih

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

尝试使用模型对象 User 从数据库中提取一些值时,出现以下错误: 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

查看用户/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 :

和 routes.php :

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

Thanks for the help

谢谢您的帮助

回答by Antonio Carlos Ribeiro

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

你不应该得到这个错误,因为看起来你正在使用 Eloquent 并且它有一个find()方法。

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:

1)User::您正在调用的与您在此处显示的不同,以检查控制器中的执行:

$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:

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

composer dumpautoload

To try to fix it.

尝试修复它。

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

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

rm -rf vendor/laravel

and install it again

并重新安装

composer update

回答by pkanane

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: Formand my db table (from a migration): forms.

有时,当 Laravel 中存在另一个同名模型并且很可能优先于您的模型时,会导致这种冲突。避免“关键词”。对我来说,我的问题是调用我的模型:Form和我的数据库表(来自迁移):forms

Running the following cmd in my FormController:

在我的FormController 中运行以下 cmd :

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

showed me that the "real" Formwas located in the Illuminate framework:

向我展示了“真实”表单位于 Illuminate 框架中:

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

So I just changed my model name to WebFormand my db table to web_forms

所以我只是将模型名称更改为WebForm,将数据库表更改为web_forms