「Laravel 9実践入門 for Mac」の感想・備忘録1 の続きCRUDの作成
環境構築 curl -s https://laravel.build/crud | bash cd crud ./vendor/bin/sail up -d vi config/app.php 以下を修正。'timezone' => 'Asia/Tokyo', 'local' => 'ja',
./vendor/bin/sail composer require jeroennoten/laravel-adminlte ./vendor/bin/sail artisan adminlte:install ./vendor/bin/sail artisan make:migration create_products_table createメソッドに追加$table->string('name');
$table->integer('price');
./vendor/bin/sail artisan migrate ./vendor/bin/sail artisan make:model Product ./vendor/bin/sail artisan make:controller ProductController -r ※ -rはリソース用コントローラ vi routes/web.phpuse App\Http\Controllers\ProductController;
とRoute::resource('product', ProductController::class);
を追加。
一覧ページの作成 mkdir resources/views/product touch resources/views/product/index.blade.php @extends('adminlte::page')
@section('title', '商品一覧')
@section('content_header')
<h1>商品一覧</h1>
@stop
@section('content')
{{-- 完了メッセージ --}}
@if (session('message'))
<div class="alert alert-info alert-dismissible">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">
×
</button>
{{ session('message') }}
</div>
@endif
{{-- 新規登録画面へ --}}
<a class="btn btn-primary mb-2" href="{{ route('product.create') }}" role="button">新規登録</a>
<div class="card">
<div class="card-body">
<table class="table table-bordered">
<thead>
<tr>
<th>商品名</th>
<th>価格</th>
<th style="width: 70px"></th>
</tr>
</thead>
<tbody>
@foreach ($products as $product)
<tr>
<td>{{ $product->name }}</td>
{{-- 数字フォーマット --}}
<td>{{ number_format($product->price) }}</td>
<td>
<a class="btn btn-primary btn-sm mb-2" href="{{ route('product.edit', $product->id) }}"
role="button">編集</a>
<form action="{{ route('product.destroy', $product->id) }}" method="post">
@csrf
@method('DELETE')
{{-- 簡易的に確認メッセージを表示 --}}
<button type="submit" class="btn btn-danger btn-sm"
onclick="return confirm('削除してもよろしいですか?');">
削除
</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
@stop
public function index()
{
return view('product.index', ['products' => Product::all()]);
}
登録ページの作成 ./vendor/bin/sail artisan make:request ProductRequest touch resources/views/product/create.blade.php public function authorize()
{
return true; // falseをtrueに変更
}
public function rules()
{
return [
'name' => 'required|string|max:64', // 追加
'price' => 'required|integer', // 追加
];
}
@extends('adminlte::page')
@section('title', '商品登録')
@section('content_header')
<h1>商品登録</h1>
@stop
@section('content')
@if ($errors->any())
<div class="alert alert-warning alert-dismissible">
{{-- エラーの表示 --}}
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
{{-- 登録画面 --}}
<div class="card">
<form action="{{ route('product.store') }}" method="post">
@csrf
<div class="card-body">
{{-- 商品名入力 --}}
<div class="form-group">
<label for="name">商品名</label>
<input type="text" class="form-control" id="name" name="name" value="{{ old('name') }}"
placeholder="商品名" />
</div>
{{-- 価格入力 --}}
<div class="form-group">
<label for="price">価格</label>
<input type="text" class="form-control" id="price" name="price" value="{{ old('price') }}"
placeholder="価格" />
</div>
</div>
<div class="card-footer">
<div class="row">
<a class="btn btn-default" href="{{ route('product.index') }}" role="button">戻る</a>
<div class="ml-auto">
<button type="submit" class="btn btn-primary">登録</button>
</div>
</div>
</div>
</form>
</div>
@stop
protected $fillable = [
'name',
'price'
];
public function create()
{
return view('product.create');
}
public function store(ProductRequest $request)
{
(new Product())->fill($request->all())->save();
return redirect()->route('product.index')->with('message', '登録しました');
}
コメント