我正在学习 laravel 4,并决定完成关于 tutsplus 的一门名为 The MVC Mindset 的课程,并完成了在这里https://tutsplus.com/course/the-mvc-mindset-2/找到的 Models and Scoping tut 。尝试查看 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>
这是 index.blade.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>
这是routes.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');
这是 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');
}
}
这是Person.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%");
}
}