未定义的雄辩方法find($ id)/ all()

时间:2014-02-24 14:59:17

标签: php laravel eloquent

我尝试使用Laravel和Eloquent创建简单模型。 这是我的代码示例:

composer.json

"laravel/framework": "4.1.*"

routes.php文件

Route::controller('items', 'ItemsController');
Route::get('item/{item}', array('as'=>'item', 'uses'=> 'ItemsController@getView'));

php artisan route:

| | GET item/{item}    | item | ItemsController@getView    |    |    |
| | GET items          |      | ItemsController@getIndex   |    |    |

控制器/ ItemsController.php

<?php

class ItemsController extends BaseController {
    public function getIndex()
    {
        $items = Item::all();
        return View::make('items.index')
            ->with('title', 'Index show')
            ->with('items', $items);
    }

    public function getView($id)
    {
        return View::make('items.view')
            ->with('title', 'View show')
            ->with('items', Item::find($id));
    }

}

模型/ Item.php

<?php

class Item extends Eloquent {

    protected $table = 'items';

}

DB:我有MySQL表“items”有6行,例如:

+----+-----------+---------------------+
| id | name      | created_at          |
+----+-----------+---------------------+
|  4 | ironman   | 2012-04-03 10:02:44 |
|  5 | robot     | 2012-04-13 10:02:44 |
+----+-----------+---------------------+

当我尝试获取mydomain / item / 2时 拉拉维尔说:

Call to undefined method Item::find()

并获取mydomain / items /

Call to undefined method Item::all()

我错过了什么?

1 个答案:

答案 0 :(得分:0)

当我“动态”更改模型和表名时,以及当我执行大量迁移回滚时,有时会发生这种情况。尝试重命名模型名称,在大多数情况下它适用于我。