Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel : BadMethodCallException Method [find] does not exist

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

like image 680
Tarik Mokafih Avatar asked Apr 05 '14 14:04

Tarik Mokafih


2 Answers

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
like image 130
Antonio Carlos Ribeiro Avatar answered Nov 13 '22 01:11

Antonio Carlos Ribeiro


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

like image 37
pkanane Avatar answered Nov 12 '22 23:11

pkanane