戻る
HTML特殊文字変換ツール(外部サイト)

ルーターのコーディング

C:\data\quick-laravel\routes\web.php

Route::prefix('part9')->group(function(){
    Route::namespace('App\Http\Controllers')->group(function() {
        Route::get('create', 'Part9Controller@create'); // P2448
        Route::post('store', 'Part9Controller@store'); // P2448
        
        Route::get('list', 'Part9Controller@list'); // P2448
        Route::get('{id}/edit', 'Part9Controller@edit'); // P2448
        Route::patch('{id}', 'Part9Controller@update'); // P2448

        Route::get('{id}', 'Part9Controller@show'); // P2516
        Route::delete('{id}', 'Part9Controller@destroy'); // P2516
    });
}); // part9

コントローラーのコーディング

C:\data\quick-laravel\app\Http\Controllers\Part9Controller.php

<?php
      
namespace App\Http\Controllers;
      
use Illuminate\Http\Request;
use App\Models\Book;
use App\Models\Review;
use DB;

class Part9Controller extends Controller
{
  public function destroy($id)
  {
    $b = Book::findOrFail($id);
    $b->delete();
    return redirect('part9/list');
  }
  public function show($id)
  {
    return view('part9.show', ['b' => Book::findOrFail($id)]);
  }
  public function list()
  {
    $data = [ 'records' => Book::all() ];
    return view('part9.list', $data);
  }
  public function update(Request $req, $id)
  { // 
    $this->validate($req, Book::$rules);
    $b = Book::find($id);
    $b->fill($req->except('_token', '_method'))->save();
    return redirect('part9/list');
  }
  public function edit($id)
  { // 指定された書籍情報を取得
    return view('part9.edit', ['b' => Book::findOrFail($id)]);
  }
  public function create()
  { // 入力フォームの生成
    return view('part9.create');
  }
  public function store(Request $req)
  { // 詰め替え
    $this->validate($req, Book::$rules);
    $b = new Book();
    $b->fill($req->except('_token'))->save();
    return redirect('part9/create');
  }
}

モデルのコーディング

共通パス: C:\data\quick-laravel\app\Models\

Book.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Book extends Model
{
  use HasFactory;
  protected $fillable = ['isbn', 'title', 'price', 'publisher', 'published'];
  public static $rules = [
    'isbn' => 'required',
    'title' => 'required|string|max:10',
    'price' => 'integer|min:0',
    'publisher' => 'required|in:翔泳社,技術評論社,日経BP,秀和システム,インプレス',
    'published' => 'required|date',
  ];
  public function reviews()
  {
      return $this->hasMany('App\Models\Review');
  }
}

Review.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Review extends Model
{
    use HasFactory;
    public function book()
    {
        return $this->belongsTo('App\Models\Book');
    }
}

ビューのコーディング

共通パス: C:\data\quick-laravel\resources\views\

part9\create.blade.php

@extends('layouts.base')
@section('title', '書籍情報フォーム')
@section('main')
@if (count($errors) > 0)
  <ul>
    @foreach($errors->all() as $err)
    <li class="text-danger">{{ $err }}</li>
    @endforeach
  </ul>
@endif
<form method="POST" action="/part9/store">
  @csrf
  <div class="pl-2">
    <label id="isbn">ISBNコード:</label><br />
    <input id="isbn" name="isbn" type="text"
      size="15" value="{{ old('isbn') }}" />
  </div>
  <div class="pl-2">
    <label id="title">書名:</label><br />
    <input id="title" name="title" type="text"
      size="30" value="{{ old('title') }}" />
  </div>
  <div class="pl-2">
    <label id="price">価格:</label><br />
    <input id="price" name="price" type="text"
      size="5" value="{{ old('price') }}" />円
  </div>
  <div class="pl-2">
    <label id="publisher">出版社:</label><br />
    <input id="publisher" name="publisher" type="text"
      size="10" value="{{ old('publisher') }}" />
      </div>
      <div class="pl-2">
    <label id="published">刊行日:</label><br />
    <input id="published" name="published" type="text"
      size="10" value="{{ old('published') }}" />
  </div>
  <div class="pl-2">
    <input type="submit" value="送信" />
  </div>
</form>
@endsection

part9\edit.blade.php

@extends('layouts.base')
@section('title', '書籍情報フォーム(編集)')
@section('main')
@if (count($errors) > 0)
  <ul>
    @foreach($errors->all() as $err)
    <li class="text-danger">{{ $err }}</li>
    @endforeach
  </ul>
@endif
<form method="POST" action="/part9/{{ $b->id }}">
  @csrf
  @method('PATCH')
  <div class="pl-2">
    <label id="isbn">ISBNコード:</label><br />
    <input id="isbn" name="isbn" type="text"
      size="15" value="{{ old('isbn', $b->isbn) }}" />
  </div>
  <div class="pl-2">
    <label id="title">書名:</label><br />
    <input id="title" name="title" type="text"
      size="30" value="{{ old('title', $b->title) }}" />
  </div>
  <div class="pl-2">
    <label id="price">価格:</label><br />
    <input id="price" name="price" type="text"
      size="5" value="{{ old('price', $b->price) }}" />円
  </div>
  <div class="pl-2">
    <label id="publisher">出版社:</label><br />
    <input id="publisher" name="publisher" type="text"
      size="10" value="{{ old('publisher', $b->publisher) }}" />
  </div>
  <div class="pl-2">
    <label id="published">刊行日:</label><br />
    <input id="published" name="published" type="text"
      size="10" value="{{ old('published', $b->published) }}" />
  </div>
  <div class="pl-2">
    <input type="submit" value="更新" />
  </div>
</form>
@endsection

part9\list.blade.php

<!DOCTYPE html>
<html>
<head>
  <meta charset =" UTF-8"/>
  <title>速習 Laravel</title>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" />
</head>
<body>
  <table class ="table">
    <tr>
      <th>書名</th><th>価格</th><th>出版社</th><th>刊行日</th><th></th>
    </tr>
    @foreach ($records as $record) <tr>
      <td>{{ $record->title }}</td>
      <td>{{ $record->price }}円</td>
      <td>{{ $record->publisher }}</td>
      <td>{{ $record->published }}</td>
      <td><a href="/part9/{{$record->id}}/edit">編集</a>
        <a href="/part9/{{$record->id}}">削除</a></td>
    </tr>@endforeach
  </table>
</body>
</html>

part9\show.blade.php

@extends('layouts.base')
@section('title', '書籍情報フォーム(詳細)')
@section('main')
<form method="POST" action="/part9/{{ $b->id }}">
  @csrf
  @method('DELETE')
  <div class="pl-2">
    <span id="isbn">ISBNコード:</span><br />
    {{ $b->isbn }}"
  </div>
  <div class="pl-2">
    <span id="title">書名:</span><br />
    {{ $b->title }}"
  </div>
  <div class="pl-2">
    <span id="price">価格:</span><br />
    {{ $b->price }}"
  </div>
  <div class="pl-2">
    <span id="publisher">出版社:</span><br />
    {{ $b->publisher }}"
  </div>
  <div class="pl-2">
    <span id="published">刊行日:</span><br />
    {{ $b->published }}"
  </div>
  <div class="pl-2">
    <input type="submit" value="削除" />
  </div>
</form>
@endsection

layouts\base.blade.php

<!DOCTYPE html>
<html>
<head>
  <meta charset =" UTF-8"/>
  <title>@yield('title')</title>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" />
</head>
<body>
  @section('main')
  <p>既定のコンテンツです。</p>
  @show
  <hr>
  <a href="../hello/list">hello/list</a><br>
  <a href="../part9/create">part9/create</a> part9/{id}/edit
</body>
</html>
戻る