我目前正在跟踪拉拉维尔的基础教程视频,因为我尝试了另一个教程,但它没有解释拉拉维尔在更多的细节。
现在我开始看第9集(https://laracasts.com/series/laravel-5-fundamentals/episodes/9)了,但是一旦我在控制器中找到了show($id)函数,就会发现findOrFail()出错了,或者如果我只使用find(),它就不会查询和返回任何内容。
除了将“文章”更改为"Posts“之外,我更喜欢命名,我基本上遵循了这个教程。
我真的想在这一点上把头撞到墙上,因为我不知道为什么会这样。
我已经检查了我的所有语法,它似乎是有序的。此外,对索引中的Article::all();的查询还返回来自my的数据。
app/Http/Controller/PostsController.php
<?php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
class PostsController extends Controller
{
public function index()
{
$posts = Post::all();
return view('posts.index', compact('posts'));
}
public function show($id)
{
$post = Post::findOrFail($id);
return view('posts.show', compact('post'));
}
}路由/web.php
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', 'PagesController@index');
Route::get('contact', 'PagesController@contact');
Route::get('posts', 'PostsController@index');
Route::get('posts/{id}', 'PostsController@show');app/Post.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $fillable = [
'title',
'body'
];
}使用findOrFail()时收到的错误
(2/2) NotFoundHttpException
No query results for model [App\Post] 1
in Handler.php line 131谢谢,我希望有人能看到我哪里出错了!
发布于 2019-04-09 01:59:52
我的表中没有ID为“1”的条目
我觉得很蠢..。
发布于 2019-04-09 04:07:34
如果您为post表设置了id的主键:
app/Post.php
<?php
namespace App;
use Illuminate\Database\Eloquent\model;
/**
*
*/
class Post extends Model
{
protected $primaryKey = 'id';
public $timestamps = false;
protected $table = 'post'; //your table name
}发布于 2019-04-09 00:54:37
我认为数据库表没有正确命名是可能的。该表应称为“员额”。如果它被命名为其他任何东西,则需要在Post模型中声明它:
protected $table = "name of your database table";https://stackoverflow.com/questions/55583636
复制相似问题