私は laravel 4 を学んでいて、The MVC Mindset と呼ばれる tutsplus のコースを受講することに決め、 https ://tutsplus.com/course/the-mvc-mindset-2/ にあるモデルとスコーピング 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%");
}
}