teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

Modelを追加しました。

2021/11/16 10:25

投稿

mimitaro
mimitaro

スコア0

title CHANGED
File without changes
body CHANGED
@@ -19,18 +19,31 @@
19
19
  // 商品登録
20
20
  Route::post('/product/store', 'ProductController@exeStore')->name('store');
21
21
 
22
- // 商品詳細画面を表示
22
+ ```
23
- Route::get('/product/detail/{id}', 'ProductController@showDetail')->name('detail');
24
23
 
24
+ ```Model
25
- // 商品編集画面を表示
25
+ <?php
26
- Route::get('/product/edit/edit/{id}', 'ProductController@showEdit')->name('edit');
27
26
 
28
- // 商品更新
27
+ namespace App\Models;
29
- Route::post('/product/update', 'ProductController@exeUpdate')->name('update');
30
28
 
31
- // 商品削除
32
- Route::post('/product/delete/{id}', 'ProductController@exeDelete')->name('delete');
29
+ use Illuminate\Database\Eloquent\Model;
33
30
 
31
+ class Product extends Model
32
+ {
33
+ //テーブル名
34
+ protected $table = 'products';
35
+
36
+ // 可変項目
37
+ protected $fillable =
38
+ [
39
+ 'company_id',
40
+ 'product_name',
41
+ 'price',
42
+ 'stock',
43
+ 'comment'
44
+ ];
45
+ }
46
+
34
47
  ```
35
48
 
36
49
  ```Controller

1

どこから壊れたのかわからないので、RouteとControllerをすべてコピペしました。

2021/11/16 10:25

投稿

mimitaro
mimitaro

スコア0

title CHANGED
File without changes
body CHANGED
@@ -9,13 +9,79 @@
9
9
  ```
10
10
 
11
11
  ```Route
12
+
13
+ // 商品一覧画面を表示
14
+ Route::get('/product', 'ProductController@showList')->name('products');
15
+
16
+ // 商品登録画面を表示
17
+ Route::get('/product/create', 'ProductController@showCreate')->name('create');
18
+
12
19
  // 商品登録
13
20
  Route::post('/product/store', 'ProductController@exeStore')->name('store');
14
21
 
22
+ // 商品詳細画面を表示
23
+ Route::get('/product/detail/{id}', 'ProductController@showDetail')->name('detail');
24
+
25
+ // 商品編集画面を表示
26
+ Route::get('/product/edit/edit/{id}', 'ProductController@showEdit')->name('edit');
27
+
28
+ // 商品更新
29
+ Route::post('/product/update', 'ProductController@exeUpdate')->name('update');
30
+
31
+ // 商品削除
32
+ Route::post('/product/delete/{id}', 'ProductController@exeDelete')->name('delete');
33
+
15
34
  ```
16
35
 
17
36
  ```Controller
37
+ <?php
38
+
39
+ namespace App\Http\Controllers;
40
+
41
+ use Illuminate\Http\Request;
42
+ use App\Models\Product;
43
+ use App\Http\Requests\ProductRequest;
44
+
45
+ class ProductController extends Controller
46
+ {
18
- /**
47
+ /**
48
+ * 商品一覧を表示する
49
+ *
50
+ * @return view
51
+ */
52
+ public function showList()
53
+ {
54
+ $products = Product::all();
55
+ return view('product.list', ['products' => $products]);
56
+ }
57
+
58
+ /**
59
+ * 商品詳細を表示する
60
+ * @param int $id
61
+ * @return view
62
+ */
63
+ public function showDetail($id)
64
+ {
65
+ $product = Product::find($id);
66
+
67
+ if (is_null($product)) {
68
+ \Session::flash('err_msg', 'データがありません。');
69
+ return redirect(route('products'));
70
+ }
71
+ return view('detail.detail', ['product' => $product]);
72
+ }
73
+
74
+ /**
75
+ * 商品登録画面を表示する
76
+ *
77
+ * @return view
78
+ */
79
+ public function showCreate()
80
+ {
81
+ return view('create.create');
82
+ }
83
+
84
+ /**
19
85
  * 商品を登録する
20
86
  *
21
87
  * @return view
@@ -24,6 +90,7 @@
24
90
  {
25
91
  // 商品のデータを受け取る
26
92
  $inputs = $request->all();
93
+ dd($inputs);
27
94
 
28
95
  \DB::beginTransaction();
29
96
  try {
@@ -39,10 +106,109 @@
39
106
  return redirect(route('products'));
40
107
  }
41
108
 
109
+ /**
110
+ * 商品編集フォームを表示する
111
+ * @param int $id
112
+ * @return view
113
+ */
114
+ public function showEdit($id)
115
+ {
116
+ $product = Product::find($id);
117
+
118
+ if (is_null($product)) {
119
+ \Session::flash('err_msg', 'データがありません。');
120
+ return redirect(route('products'));
121
+ }
122
+ return view('edit.edit', ['product' => $product]);
123
+ }
124
+
125
+ /**
126
+ * 商品を更新する
127
+ *
128
+ * @return view
129
+ */
130
+ public function exeUpdate(ProductRequest $request)
131
+ {
132
+ // 商品のデータを受け取る
133
+ $inputs = $request->all();
134
+
135
+ \DB::beginTransaction();
136
+ try {
137
+ // 商品を更新
138
+ $product = Product::find($inputs['id']);
139
+
140
+ $product->fill([
141
+ 'product_name' => $inputs['product_name'],
142
+ 'price' => $inputs['price'],
143
+ 'stock' => $inputs['stock'],
144
+ 'comment' => $inputs['comment']
145
+ ]);
146
+ $product->save();
147
+
148
+ \DB::commit();
149
+ } catch(\Throwable $e) {
150
+ \DB::rollback();
151
+ abort(500);
152
+ }
153
+
154
+ \Session::flash('err_msg', '商品を更新しました。');
155
+ return redirect(route('products'));
156
+ }
157
+
158
+ /**
159
+ * 商品を削除する
160
+ * @param int $id
161
+ * @return view
162
+ */
163
+ public function exeDelete($id)
164
+ {
165
+ if (empty($id)) {
166
+ \Session::flash('err_msg', 'データがありません。');
167
+ return redirect(route('products'));
168
+ }
169
+
170
+ try {
171
+ // 商品を削除
172
+ Product::destroy($id);
173
+ } catch(\Throwable $e) {
174
+ abort(500);
175
+ }
176
+
177
+ \Session::flash('err_msg', '商品を削除しました。');
178
+ return redirect(route('products'));
179
+ }
180
+ }
181
+
42
182
  ```
43
183
 
44
- ```view
184
+ ```layout
185
+ <!DOCTYPE HTML>
186
+ <html lang="ja">
187
+ <head>
188
+ <meta charset="UTF-8">
189
+ <meta name="csrf-token" content="{{ csrf_token() }}">
190
+ <title>@yield('title')</title>
191
+ <link rel="stylesheet" href="/css/app.css">
192
+ <script src="/js/app.js" defer></script>
193
+ </head>
194
+ <body>
195
+ <header>
196
+ @include('create.header')
197
+ </header>
198
+ <br>
199
+ <div class="container">
200
+ @yield('create.content')
201
+ </div>
202
+ <footer class="footer bg-dark fixed-bottom">
203
+ @include('product.footer')
204
+ </footer>
205
+ </body>
206
+ </html>
45
207
 
208
+ ```
209
+
210
+ ```create
211
+
46
212
  @extends('create.layout')
47
213
  @section('title', '商品登録')
48
214
  @section('create.content')