我刚开始尝试使用Laravel,但是我在通过quickstart guide时遇到了问题,特别是使用了用户模型。在我尝试使用User模型访问数据库并在视图中显示条目之前,指南中的所有内容都适用于我。当我尝试访问浏览器中的公共/用户/时,我遇到了一个页面,上面写着哎呀,看起来出了问题。。我的代码有什么问题?
我还有一个视图文件 users.blade.php ,如下所示:
@extends('layout')
@section('content')
@foreach($users as $user)
<p>{{ $user->name }}</p>
@endforeach
@stop
这是我的整个 routes.php 文件。我应该注意,当users.blade.php只是静态html时,当我的路由文件只有return View::make('users')
时,它会在我的浏览器中显示正常:
<?php
Route::get('/', function()
{
return View::make('hello');
});
Route::get('users',function()
{
$users = User::all();
return View::make('users')->with('users',$users);
//return View::make('users');
});
(这是我启动项目时自动生成的)
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password', 'remember_token');
}
我也试过让我的User.php模型看起来像
class User extends Eloquent {}
正如快速入门指南所说,但这也会导致同样的错误。
这是我的 composer.json 文件:
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
"laravel/framework": "4.2.*"
},
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
]
},
"scripts": {
"post-install-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"post-update-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"post-create-project-cmd": [
"php artisan key:generate"
]
},
"config": {
"preferred-install": "dist"
},
"minimum-stability": "stable"
}