【发布时间】:2014-03-26 07:47:38
【问题描述】:
我尝试使用 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工匠路线:
| | 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:我有 6 行的 MySQL 表“项目”,例如:
+----+-----------+---------------------+
| id | name | created_at |
+----+-----------+---------------------+
| 4 | ironman | 2012-04-03 10:02:44 |
| 5 | robot | 2012-04-13 10:02:44 |
+----+-----------+---------------------+
当我尝试 GET mydomain/item/2 Laravel 说:
Call to undefined method Item::find()
然后 GET mydomain/items/
Call to undefined method Item::all()
我错过了什么?
【问题讨论】:
-
你确定你使用的是 Eloquent 类吗?我不知道 Laravel 但 Eloquent 类应该有 find() 和 all() 方法,除非它定义你必须编写这些方法。 Laravel 需要
use namespace\Eloquent语句吗? -
你做了
composer dump-autoload吗? -
@Jorge Laravel 自己做