LoginSignup
1
0

Laravel で MariaDB の CRUD (その 2)

Last updated at Posted at 2018-06-14

こちらのページの続きです。

Laravel で MariaDB の CRUD (その 1)
MariaDB の テーブル article の全データを JSON で表示するところまでできています。
そこから、テーブル表示が出来るようにします。

  1. ビューを用意
  2. mkdir resources/views/layouts
    
    resources/views/layouts/application.blade.php
    <!DOCTYPE html>
    <html lang="ja">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>@yield('title')</title>
    </head>
    <body>
      @yield('content')
    </body>
    </html>
    
    mkdir resources/views/articles
    
    resources/views/articles/index.blade.php
    {{-- layoutsフォルダのapplication.blade.phpを継承 --}}
    @extends('layouts.application')
    
    {{-- @yield('title')にテンプレートごとにtitleタグの値を代入 --}}
    @section('title', '記事一覧')
    
    {{-- application.blade.phpの@yield('content')に以下のレイアウトを代入 --}}
    @section('content')
    <h1>こんにちは</h1>
    @endsection
    
  3. コントローラーを修正
  4. app/Http/Controllers/ArticlesController.php
        public function index()
        {
            //
     $articles = Article::all();
    //      return $articles;       // コメントアウト
      return view('articles.index'); // 追加 
        }
    
  5. サーバーを起動
  6.  この段階での表示の確認
    php artisan serve
    

    ブラウザーで、
    http://localhost:8000/articles
    にアクセス
    laravel_jun1401.png

  7. ビューの修正
  8. resources/views/articles/index.blade.php
    {{-- layoutsフォルダのapplication.blade.phpを継承 --}}
    @extends('layouts.application')
    
    {{-- @yield('title')にテンプレートごとにtitleタグの値を代入 --}}
    @section('title', '記事一覧')
    
    {{-- application.blade.phpの@yield('content')に以下のレイアウトを代入 --}}
    @section('content')
    <table>
     @foreach ($articles as $article)
            <tr>
        <td>{{$article->title}}</td>
        <td>{{$article->body}}</td>
            </tr>
      @endforeach
    </table>
    @endsection
    
  9. コントローラーの修正
  10. app/Http/Controllers/ArticlesController.php
    // 略
        public function index()
        {
            //
     $articles = Article::all();
       return view('articles.index', ['articles' => $articles]);
        }
    // 略
    
  11. サーバーを起動してブラウザーで確認
  12. >http://localhost:8000/articles ![laravel_jun1402.png](https://qiita-image-store.s3.amazonaws.com/0/179446/47e8169b-477d-7c65-9dc3-4b05258e0a18.png)

確認したバージョン

$ php artisan --version
Laravel Framework 10.37.3

次のページ

Laravel で MariaDB の CRUD (その 3)

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0