Laravel의 MariaDB CRUD (3)

다음 페이지의 계속입니다.

Laravel의 MariaDB CRUD (2)
상세 페이지를 만듭니다.

1) 라우팅 확인

articles.show가 있는지 확인
php artisan route:list
$ php artisan route:list
+--------+-----------+-------------------------+------------------+-------------------------------------------------+------------+
| Domain | Method    | URI                     | Name             | Action                                          | Middleware |
+--------+-----------+-------------------------+------------------+-------------------------------------------------+------------+
|        | GET|HEAD  | /                       |                  | Closure                                         | web        |
|        | GET|HEAD  | api/user                |                  | Closure                                         | api        |
|        |           |                         |                  |                                                 | auth:api   |
|        | GET|HEAD  | articles                | articles.index   | App\Http\Controllers\ArticlesController@index   | web        |
|        | POST      | articles                | articles.store   | App\Http\Controllers\ArticlesController@store   | web        |
|        | GET|HEAD  | articles/create         | articles.create  | App\Http\Controllers\ArticlesController@create  | web        |
|        | GET|HEAD  | articles/{article}      | articles.show    | App\Http\Controllers\ArticlesController@show    | web        |
|        | PUT|PATCH | articles/{article}      | articles.update  | App\Http\Controllers\ArticlesController@update  | web        |
|        | DELETE    | articles/{article}      | articles.destroy | App\Http\Controllers\ArticlesController@destroy | web        |
|        | GET|HEAD  | articles/{article}/edit | articles.edit    | App\Http\Controllers\ArticlesController@edit    | web        |
+--------+-----------+-------------------------+------------------+-------------------------------------------------+------------+

2) 일람 페이지에 상세 페이지에의 링크를 붙인다

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>
        <td><a href="/articles/{{$article->id}}">詳細を表示</a></td>
        </tr>
  @endforeach
</table>
@endsection

3) show 액션 편집

app/Http/Controllers/ArticlesController.php
// 略
    public function show($id)
    {
 // 引数で受け取った$idを元にfindでレコードを取得
  $article = Article::find($id);
  // viewにデータを渡す
  return view('articles.show', ['article' => $article]);
    }
// 略

4) 상세 페이지의 뷰 작성

resources/views/articles/show.blade.php
{{-- layoutsフォルダのapplication.blade.phpを継承 --}}
@extends('layouts.application')

{{-- @yield('title')にテンプレートごとの値を代入 --}}
@section('title', '記事詳細')

{{-- application.blade.phpの@yield('content')に以下のレイアウトを代入 --}}
@section('content')
  <h1>{{$article->title}}</h1>
  <p>{{$article->body}}</p>
  <br><br>
  <a href="/articles">一覧に戻る</a>
@endsection

5) 서버를 시작하고 브라우저에서 확인
php artisan serve

http://localhost:8000/articles


상세보기를 클릭하면


다음 페이지
Laravel에서 MariaDB의 CRUD (그 4 Create)

좋은 웹페이지 즐겨찾기