laravel 4 路由、模型、控制器错误 Illuminate Database Eloquent


laravel 4 routing, models, controllers error Illuminate Database Eloquent ModelNotFoundException

我正在学习 laravel 4,并决定完成一门名为 MVC 心态的 tutsplus 课程,并完成了 https://tutsplus.com/course/the-mvc-mindset-2/在这里找到的模型和范围界定图特。我在尝试查看 create.blade.php 文件时出错。我在下面收到此错误。

试图查看我出错的地方,因为我之前能够在此页面上应用 CRUD,但是当我输入搜索查询方面时,它导致了所有这些错误。

希望有人能帮忙!!

Illuminate ' Database ' Eloquent ' ModelNotFoundException. 
open: C:'xampp'htdocs'mvc'vendor'laravel'framework'src'Illuminate'Database'Eloquent'Model.php
* @param array $columns
* @return 'Illuminate'Database'Eloquent'Model|Collection|static
*/
public static function findOrFail($id, $columns = array('*'))
{
if ( ! is_null($model = static::find($id, $columns))) return $model;
throw new ModelNotFoundException;
}

这是create.blade.php

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Create</title>
</head>
<body>
<form method="post" action="/names">
<label for="name">Name to add:</label>
<input name="name" id="name" type="text">
<div>
<button type="submit">Submit</button> 
</div>
</form>
</body>
</html>

这是索引刀片.php

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<h1>All Names</h1>
<p> <a href="/names/create">Add a New name</a></p>
<ul>
@foreach ($folks as $person)
<li> 
<a href="names/{{$person->id}}">
{{ $person->name }} 
</a>
</li>
@endforeach
</ul>
</body>
</html>

这是路线.php

<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
|
*/
//Route::get('/', function()
//{
// return View::make('hello');
//});
//Route::get('names/search/{letter}', function($letter)
//{
// return Person::byFirstLetter($letter)->get();
// //return Person::where('name','LIKE', "$query%")->get();
//});
Route::get('names/{id}', 'NamesController@getShow');
Route::controller('names', 'NamesController');

这是名称控制器.php

<?php
class NamesController extends BaseController {
public function getIndex()
{
$folks = Person::all();
return View::make('names.index')
->with('folks', $folks);
}
public function postIndex()
{
$name = Input::get('name');
Person::create(['name' => $name]);
return Redirect::to('/names');
}
public function getShow($id)
{
$person = Person::findOrFail($id);
//old way without using models
//$person = DB::table('people')->find($id);
//if (null == $person) return Redirect::to('names');
return $person->name;
}
public function getCreate()
{
return View::make('names.create');
}

}

这是人.php

<?php
class Person extends Eloquent {
//protected $guarded= array();
protected $guarded=[];
public $timestamps = false;
public function scopeByFirstLetter($query, $letter)
{
return $query->where('name','LIKE', "$letter%");
}
}

你能尝试在模型上提到$table属性吗?

class Person extends Eloquent {
    protected $table ='TABLE_NAME_GOES_HERE';
}

根据文档

类的小写复数名称将用作表名称,除非显式指定了其他名称。因此,在这种情况下,Eloquent 将假定用户模型将记录存储在用户表中。您可以通过在模型上定义表属性来指定自定义表

因此,请确保您具有文档中提到的表名。否则,请指定 table 属性。由于您的模型Person它将查找表persons。确保数据库中有此内容。