【发布时间】:2015-05-28 18:13:54
【问题描述】:
我是 Laravel 5 和 MVC 世界的新手。
我已经安装了 Laravel 5 样板项目,一切正常,开箱即用。我可以注册然后登录到应用程序。
我有点理解路由、控制器、模型和视图的角色,但我无法弄清楚从用户数据库表中检索数据并将其显示在我的主视图中的语法(使用 HomeController)。
我不确定哪段代码放在哪里,以及语法应该如何编码。在下面的代码中,我以用户身份登录,我只是试图显示用户表中的所有用户。
你能帮忙吗?如果我能理解如何从我的数据库中获取数据到我的视图中,我会很高兴。我正在寻找简单的例子。我在网上看到了很多示例,但对于大多数人来说,我什至不知道在哪里(控制器、路由等)复制提供的代码。
下面的代码出现以下错误:
FatalErrorException in HomeController.php line 52: Class 'App\Http\Controllers\User' not found
routes.php
Route::get('home', 'HomeController@index');
HomeController.php
<?php namespace App\Http\Controllers;
class HomeController extends Controller {
/*
|--------------------------------------------------------------------------
| Home Controller
|--------------------------------------------------------------------------
|
| This controller renders your application's "dashboard" for users that
| are authenticated. Of course, you are free to change or remove the
| controller as you wish. It is just here to get your app started!
|
*/
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard to the user.
*
* @return Response
*/
public function index()
{
$users = User::all();
return View::make('user.index', ['users' => $users]);
}
Home.blade.php
@extends('app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="panel panel-default">
<div class="panel-heading">List of users</div>
<div class="panel-body">
<table>
@foreach($users as $key => $value)
<tr>
<td>{{ $value->id }}</td>
<td>{{ $value->name }}</td>
<td>{{ $value->email }}</td>
</tr>
@endforeach
</table>
</div>
</div>
</div>
</div>
</div>
@endsection
【问题讨论】:
标签: model-view-controller laravel-5