質問編集履歴

2

Modelを追加しました。

2021/11/16 10:25

投稿

mimitaro
mimitaro

スコア0

test CHANGED
File without changes
test CHANGED
@@ -40,34 +40,60 @@
40
40
 
41
41
 
42
42
 
43
- // 商品詳細画面を表示
44
-
45
- Route::get('/product/detail/{id}', 'ProductController@showDetail')->name('detail');
46
-
47
-
48
-
49
- // 商品編集画面を表示
50
-
51
- Route::get('/product/edit/edit/{id}', 'ProductController@showEdit')->name('edit');
52
-
53
-
54
-
55
- // 商品更新
56
-
57
- Route::post('/product/update', 'ProductController@exeUpdate')->name('update');
58
-
59
-
60
-
61
- // 商品削除
62
-
63
- Route::post('/product/delete/{id}', 'ProductController@exeDelete')->name('delete');
64
-
65
-
66
-
67
43
  ```
68
44
 
69
45
 
70
46
 
47
+ ```Model
48
+
49
+ <?php
50
+
51
+
52
+
53
+ namespace App\Models;
54
+
55
+
56
+
57
+ use Illuminate\Database\Eloquent\Model;
58
+
59
+
60
+
61
+ class Product extends Model
62
+
63
+ {
64
+
65
+ //テーブル名
66
+
67
+ protected $table = 'products';
68
+
69
+
70
+
71
+ // 可変項目
72
+
73
+ protected $fillable =
74
+
75
+ [
76
+
77
+ 'company_id',
78
+
79
+ 'product_name',
80
+
81
+ 'price',
82
+
83
+ 'stock',
84
+
85
+ 'comment'
86
+
87
+ ];
88
+
89
+ }
90
+
91
+
92
+
93
+ ```
94
+
95
+
96
+
71
97
  ```Controller
72
98
 
73
99
  <?php

1

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

2021/11/16 10:25

投稿

mimitaro
mimitaro

スコア0

test CHANGED
File without changes
test CHANGED
@@ -20,19 +20,151 @@
20
20
 
21
21
  ```Route
22
22
 
23
+
24
+
25
+ // 商品一覧画面を表示
26
+
27
+ Route::get('/product', 'ProductController@showList')->name('products');
28
+
29
+
30
+
31
+ // 商品登録画面を表示
32
+
33
+ Route::get('/product/create', 'ProductController@showCreate')->name('create');
34
+
35
+
36
+
23
37
  // 商品登録
24
38
 
25
39
  Route::post('/product/store', 'ProductController@exeStore')->name('store');
26
40
 
27
41
 
28
42
 
43
+ // 商品詳細画面を表示
44
+
45
+ Route::get('/product/detail/{id}', 'ProductController@showDetail')->name('detail');
46
+
47
+
48
+
49
+ // 商品編集画面を表示
50
+
51
+ Route::get('/product/edit/edit/{id}', 'ProductController@showEdit')->name('edit');
52
+
53
+
54
+
55
+ // 商品更新
56
+
57
+ Route::post('/product/update', 'ProductController@exeUpdate')->name('update');
58
+
59
+
60
+
61
+ // 商品削除
62
+
63
+ Route::post('/product/delete/{id}', 'ProductController@exeDelete')->name('delete');
64
+
65
+
66
+
29
67
  ```
30
68
 
31
69
 
32
70
 
33
71
  ```Controller
34
72
 
73
+ <?php
74
+
75
+
76
+
77
+ namespace App\Http\Controllers;
78
+
79
+
80
+
81
+ use Illuminate\Http\Request;
82
+
83
+ use App\Models\Product;
84
+
85
+ use App\Http\Requests\ProductRequest;
86
+
87
+
88
+
89
+ class ProductController extends Controller
90
+
91
+ {
92
+
35
- /**
93
+ /**
94
+
95
+ * 商品一覧を表示する
96
+
97
+ *
98
+
99
+ * @return view
100
+
101
+ */
102
+
103
+ public function showList()
104
+
105
+ {
106
+
107
+ $products = Product::all();
108
+
109
+ return view('product.list', ['products' => $products]);
110
+
111
+ }
112
+
113
+
114
+
115
+ /**
116
+
117
+ * 商品詳細を表示する
118
+
119
+ * @param int $id
120
+
121
+ * @return view
122
+
123
+ */
124
+
125
+ public function showDetail($id)
126
+
127
+ {
128
+
129
+ $product = Product::find($id);
130
+
131
+
132
+
133
+ if (is_null($product)) {
134
+
135
+ \Session::flash('err_msg', 'データがありません。');
136
+
137
+ return redirect(route('products'));
138
+
139
+ }
140
+
141
+ return view('detail.detail', ['product' => $product]);
142
+
143
+ }
144
+
145
+
146
+
147
+ /**
148
+
149
+ * 商品登録画面を表示する
150
+
151
+ *
152
+
153
+ * @return view
154
+
155
+ */
156
+
157
+ public function showCreate()
158
+
159
+ {
160
+
161
+ return view('create.create');
162
+
163
+ }
164
+
165
+
166
+
167
+ /**
36
168
 
37
169
  * 商品を登録する
38
170
 
@@ -50,6 +182,8 @@
50
182
 
51
183
  $inputs = $request->all();
52
184
 
185
+ dd($inputs);
186
+
53
187
 
54
188
 
55
189
  \DB::beginTransaction();
@@ -80,11 +214,209 @@
80
214
 
81
215
 
82
216
 
217
+ /**
218
+
219
+ * 商品編集フォームを表示する
220
+
221
+ * @param int $id
222
+
223
+ * @return view
224
+
225
+ */
226
+
227
+ public function showEdit($id)
228
+
229
+ {
230
+
231
+ $product = Product::find($id);
232
+
233
+
234
+
235
+ if (is_null($product)) {
236
+
237
+ \Session::flash('err_msg', 'データがありません。');
238
+
239
+ return redirect(route('products'));
240
+
241
+ }
242
+
243
+ return view('edit.edit', ['product' => $product]);
244
+
245
+ }
246
+
247
+
248
+
249
+ /**
250
+
251
+ * 商品を更新する
252
+
253
+ *
254
+
255
+ * @return view
256
+
257
+ */
258
+
259
+ public function exeUpdate(ProductRequest $request)
260
+
261
+ {
262
+
263
+ // 商品のデータを受け取る
264
+
265
+ $inputs = $request->all();
266
+
267
+
268
+
269
+ \DB::beginTransaction();
270
+
271
+ try {
272
+
273
+ // 商品を更新
274
+
275
+ $product = Product::find($inputs['id']);
276
+
277
+
278
+
279
+ $product->fill([
280
+
281
+ 'product_name' => $inputs['product_name'],
282
+
283
+ 'price' => $inputs['price'],
284
+
285
+ 'stock' => $inputs['stock'],
286
+
287
+ 'comment' => $inputs['comment']
288
+
289
+ ]);
290
+
291
+ $product->save();
292
+
293
+
294
+
295
+ \DB::commit();
296
+
297
+ } catch(\Throwable $e) {
298
+
299
+ \DB::rollback();
300
+
301
+ abort(500);
302
+
303
+ }
304
+
305
+
306
+
307
+ \Session::flash('err_msg', '商品を更新しました。');
308
+
309
+ return redirect(route('products'));
310
+
311
+ }
312
+
313
+
314
+
315
+ /**
316
+
317
+ * 商品を削除する
318
+
319
+ * @param int $id
320
+
321
+ * @return view
322
+
323
+ */
324
+
325
+ public function exeDelete($id)
326
+
327
+ {
328
+
329
+ if (empty($id)) {
330
+
331
+ \Session::flash('err_msg', 'データがありません。');
332
+
333
+ return redirect(route('products'));
334
+
335
+ }
336
+
337
+
338
+
339
+ try {
340
+
341
+ // 商品を削除
342
+
343
+ Product::destroy($id);
344
+
345
+ } catch(\Throwable $e) {
346
+
347
+ abort(500);
348
+
349
+ }
350
+
351
+
352
+
353
+ \Session::flash('err_msg', '商品を削除しました。');
354
+
355
+ return redirect(route('products'));
356
+
357
+ }
358
+
359
+ }
360
+
361
+
362
+
83
363
  ```
84
364
 
85
365
 
86
366
 
367
+ ```layout
368
+
369
+ <!DOCTYPE HTML>
370
+
371
+ <html lang="ja">
372
+
373
+ <head>
374
+
375
+ <meta charset="UTF-8">
376
+
377
+ <meta name="csrf-token" content="{{ csrf_token() }}">
378
+
379
+ <title>@yield('title')</title>
380
+
381
+ <link rel="stylesheet" href="/css/app.css">
382
+
383
+ <script src="/js/app.js" defer></script>
384
+
385
+ </head>
386
+
387
+ <body>
388
+
389
+ <header>
390
+
391
+ @include('create.header')
392
+
393
+ </header>
394
+
395
+ <br>
396
+
397
+ <div class="container">
398
+
399
+ @yield('create.content')
400
+
401
+ </div>
402
+
403
+ <footer class="footer bg-dark fixed-bottom">
404
+
405
+ @include('product.footer')
406
+
407
+ </footer>
408
+
409
+ </body>
410
+
411
+ </html>
412
+
413
+
414
+
415
+ ```
416
+
417
+
418
+
87
- ```view
419
+ ```create
88
420
 
89
421
 
90
422