牛骨文教育服务平台(让学习变的简单)

5.8 列出所有文章

我们还要列出所有文章,对应的路由是:

GET|HEAD articles | articles.index | ArticlesController@index

在 app/controllers/ArticlesController.php 文件中,修改 ArticlesController 控制器 index 动作:

    public function index()
    {
        $articles = Article::all();

        return View::make("articles.index", compact("articles"));
    }

然后编写这个动作的视图,保存为 app/views/articles/index.blade.php:

<h1>Listing articles</h1>

<table>
  <tr>
    <th>Title</th>
    <th>Text</th>
  </tr>

  @foreach ($articles as $article)
    <tr>
      <td>{{ $article->title }}</td>
      <td>{{ $article->text }}</td>
    </tr>
  @endforeach
</table>

现在访问 http://localhost:8000/articles ,会看到已经发布的文章列表。