回答編集履歴

3

戻しておく

2020/01/08 13:39

投稿

退会済みユーザー
test CHANGED
@@ -1 +1,405 @@
1
+ 自分ならこうしますという形で、回答します。
2
+
3
+ # ER
4
+
5
+ ![イメージ説明](977f5b38bc8913fffe82bed7c9535db3.png)
6
+
7
+ # migrations
8
+
9
+ ```php
10
+
11
+ <?php
12
+
13
+ use Illuminate\Database\Migrations\Migration;
14
+
15
+ use Illuminate\Database\Schema\Blueprint;
16
+
17
+ use Illuminate\Support\Facades\Schema;
18
+
19
+ class CreateRecipesTable extends Migration
20
+
21
+ {
22
+
23
+ /**
24
+
25
+ * Run the migrations.
26
+
27
+ *
28
+
29
+ * @return void
30
+
31
+ */
32
+
33
+ public function up()
34
+
35
+ {
36
+
37
+ Schema::create('recipes', function (Blueprint $table) {
38
+
39
+ $table->bigIncrements('id');
40
+
41
+ $table->string('name');
42
+
43
+ $table->unsignedBigInteger('created_by');
44
+
45
+ $table->timestamps();
46
+
47
+ $table->foreign('created_by')->references('id')->on('users');
48
+
49
+ });
50
+
51
+ }
52
+
53
+ /**
54
+
55
+ * Reverse the migrations.
56
+
57
+ *
58
+
59
+ * @return void
60
+
61
+ */
62
+
63
+ public function down()
64
+
65
+ {
66
+
67
+ Schema::dropIfExists('recipes');
68
+
69
+ }
70
+
71
+ }
72
+
73
+ ```
74
+
75
+ ```php
76
+
77
+ <?php
78
+
79
+ use Illuminate\Database\Migrations\Migration;
80
+
81
+ use Illuminate\Database\Schema\Blueprint;
82
+
83
+ use Illuminate\Support\Facades\Schema;
84
+
1
- 時間かけて書いた回答に対して、質問者からノーリアクションで、ナメてると思うので消した。
85
+ class CreateCommentsTable extends Migration
86
+
87
+ {
88
+
89
+ /**
90
+
91
+ * Run the migrations.
92
+
93
+ *
94
+
95
+ * @return void
96
+
97
+ */
98
+
99
+ public function up()
100
+
101
+ {
102
+
103
+ Schema::create('comments', function (Blueprint $table) {
104
+
105
+ $table->bigIncrements('id');
106
+
107
+ $table->unsignedBigInteger('recipe_id');
108
+
109
+ $table->string('content');
110
+
111
+ $table->unsignedBigInteger('created_by');
112
+
113
+ $table->timestamps();
114
+
115
+ $table->foreign('recipe_id')->references('id')->on('recipes');
116
+
117
+ $table->foreign('created_by')->references('id')->on('users');
118
+
119
+ });
120
+
121
+ }
122
+
123
+ /**
124
+
125
+ * Reverse the migrations.
126
+
127
+ *
128
+
129
+ * @return void
130
+
131
+ */
132
+
133
+ public function down()
134
+
135
+ {
136
+
137
+ Schema::dropIfExists('comments');
138
+
139
+ }
140
+
141
+ }
142
+
143
+ ```
144
+
145
+ # models
146
+
147
+ ```php
148
+
149
+ <?php
150
+
151
+ namespace App;
152
+
153
+ use Illuminate\Database\Eloquent\Model;
154
+
155
+ use Illuminate\Database\Eloquent\Relations\BelongsTo;
156
+
157
+ use Illuminate\Database\Eloquent\Relations\HasMany;
158
+
159
+ class Recipe extends Model
160
+
161
+ {
162
+
163
+ protected $fillable = [
164
+
165
+ 'name',
166
+
167
+ 'createdBy'
168
+
169
+ ];
170
+
171
+ /**
172
+
173
+ * @return HasMany
174
+
175
+ */
176
+
177
+ public function comments()
178
+
179
+ {
180
+
181
+ return $this->hasMany(Comment::class);
182
+
183
+ }
184
+
185
+ /**
186
+
187
+ * @return BelongsTo
188
+
189
+ */
190
+
191
+ public function createdBy()
192
+
193
+ {
194
+
195
+ return $this->belongsTo(User::class, 'created_by');
196
+
197
+ }
198
+
199
+ }
200
+
201
+ ```
202
+
203
+ ```php
204
+
205
+ <?php
206
+
207
+ namespace App;
208
+
209
+ use Illuminate\Database\Eloquent\Model;
210
+
211
+ use Illuminate\Database\Eloquent\Relations\BelongsTo;
212
+
213
+ class Comment extends Model
214
+
215
+ {
216
+
217
+ protected $fillable = [
218
+
219
+ 'content',
220
+
221
+ 'createdBy'
222
+
223
+ ];
224
+
225
+ /**
226
+
227
+ * @return BelongsTo
228
+
229
+ */
230
+
231
+ public function recipe()
232
+
233
+ {
234
+
235
+ return $this->belongsTo(Recipe::class);
236
+
237
+ }
238
+
239
+ /**
240
+
241
+ * @return BelongsTo
242
+
243
+ */
244
+
245
+ public function createdBy()
246
+
247
+ {
248
+
249
+ return $this->belongsTo(User::class, 'created_by');
250
+
251
+ }
252
+
253
+ }
254
+
255
+ ```
256
+
257
+ # controllers
258
+
259
+ ```php
260
+
261
+ <?php
262
+
263
+ namespace App\Http\Controllers;
264
+
265
+ use App\Recipe;
266
+
267
+ use Illuminate\View\View;
268
+
269
+ class RecipeController extends Controller
270
+
271
+ {
272
+
273
+ /**
274
+
275
+ * @return View
276
+
277
+ */
278
+
279
+ public function index()
280
+
281
+ {
282
+
283
+ $recipes = Recipe::query()
284
+
285
+ ->with(['comments', 'createdBy'])
286
+
287
+ ->paginate();
288
+
289
+ return view('recipes.index', compact('recipes'));
290
+
291
+ }
292
+
293
+ /**
294
+
295
+ * @param Recipe $recipe
296
+
297
+ * @return View
298
+
299
+ */
300
+
301
+ public function show(Recipe $recipe)
302
+
303
+ {
304
+
305
+ return view('recipes.show', compact('recipe'));
306
+
307
+ }
308
+
309
+ }
310
+
311
+ ```
312
+
313
+ ```php
314
+
315
+ <?php
316
+
317
+ namespace App\Http\Controllers;
318
+
319
+ use App\Comment;
320
+
321
+ use App\Recipe;
322
+
323
+ use Illuminate\Http\RedirectResponse;
324
+
325
+ use Illuminate\Http\Request;
326
+
327
+ use Illuminate\Support\Facades\Auth;
328
+
329
+ class CommentController extends Controller
330
+
331
+ {
332
+
333
+ /**
334
+
335
+ * @param Request $request
336
+
337
+ * @param Recipe $recipe
338
+
339
+ * @return RedirectResponse
340
+
341
+ */
342
+
343
+ public function store(Request $request, Recipe $recipe)
344
+
345
+ {
346
+
347
+ $recipe->comments()->save(new Comment([
348
+
349
+ 'content' => $request->get('content'),
350
+
351
+ 'created_by' => Auth::id()
352
+
353
+ ]));
354
+
355
+ return redirect()->route('recipes', compact('recipe'));
356
+
357
+ }
358
+
359
+ }
360
+
361
+ ```
362
+
363
+ # routing
364
+
365
+ ```php
366
+
367
+ Route::resource('recipes', 'RecipeController');
368
+
369
+ Route::post('comments/{recipe}', 'CommentController@store')->name('comments.store');
370
+
371
+ ```
372
+
373
+ # blade
374
+
375
+ ```php
376
+
377
+ @section('content')
378
+
379
+ <h1>レシピID:{{ $recipe->id }}の詳細ページ</h1>
380
+
381
+ @auth()
382
+
383
+ {!! Form::open(['route' => 'comments.store']) !!}
384
+
385
+ <div class="form-group">
386
+
387
+ {!! Form::textarea('content',['class' => 'form-control']) !!}
388
+
389
+ {!! Form::submit('投稿', ['class' => 'btn btn-primary btn-block']) !!}
390
+
391
+ </div>
392
+
393
+ {!! Form::close() !!}
394
+
395
+ @endauth
396
+
397
+ @foreach($recipe->comments as $comment)
398
+
399
+ <p>{{ $comment->content }}</p>
400
+
401
+ @endforeach
402
+
403
+ @endsection
404
+
405
+ ```

2

修正

2020/01/08 13:39

投稿

退会済みユーザー
test CHANGED
@@ -1,493 +1 @@
1
- 自分ならこうしますという形で、回答します。
2
-
3
-
4
-
5
- # ER
6
-
7
-
8
-
9
- ![イメージ説明](977f5b38bc8913fffe82bed7c9535db3.png)
10
-
11
-
12
-
13
- # migrations
14
-
15
-
16
-
17
- ```php
18
-
19
- <?php
20
-
21
-
22
-
23
- use Illuminate\Database\Migrations\Migration;
24
-
25
- use Illuminate\Database\Schema\Blueprint;
26
-
27
- use Illuminate\Support\Facades\Schema;
28
-
29
-
30
-
31
- class CreateRecipesTable extends Migration
32
-
33
- {
34
-
35
- /**
36
-
37
- * Run the migrations.
38
-
39
- *
40
-
41
- * @return void
42
-
43
- */
44
-
45
- public function up()
46
-
47
- {
48
-
49
- Schema::create('recipes', function (Blueprint $table) {
50
-
51
- $table->bigIncrements('id');
52
-
53
- $table->string('name');
54
-
55
- $table->unsignedBigInteger('created_by');
56
-
57
- $table->timestamps();
58
-
59
-
60
-
61
- $table->foreign('created_by')->references('id')->on('users');
62
-
63
- });
64
-
65
- }
66
-
67
-
68
-
69
- /**
70
-
71
- * Reverse the migrations.
72
-
73
- *
74
-
75
- * @return void
76
-
77
- */
78
-
79
- public function down()
80
-
81
- {
82
-
83
- Schema::dropIfExists('recipes');
84
-
85
- }
86
-
87
- }
88
-
89
- ```
90
-
91
-
92
-
93
- ```php
94
-
95
- <?php
96
-
97
-
98
-
99
- use Illuminate\Database\Migrations\Migration;
100
-
101
- use Illuminate\Database\Schema\Blueprint;
102
-
103
- use Illuminate\Support\Facades\Schema;
104
-
105
-
106
-
107
- class CreateCommentsTable extends Migration
1
+ 時間かけて書いた回答に対して、質問者からノーリアクションで、ナメてると思うので消した。
108
-
109
- {
110
-
111
- /**
112
-
113
- * Run the migrations.
114
-
115
- *
116
-
117
- * @return void
118
-
119
- */
120
-
121
- public function up()
122
-
123
- {
124
-
125
- Schema::create('comments', function (Blueprint $table) {
126
-
127
- $table->bigIncrements('id');
128
-
129
- $table->unsignedBigInteger('recipe_id');
130
-
131
- $table->string('content');
132
-
133
- $table->unsignedBigInteger('created_by');
134
-
135
- $table->timestamps();
136
-
137
-
138
-
139
- $table->foreign('recipe_id')->references('id')->on('recipes');
140
-
141
- $table->foreign('created_by')->references('id')->on('users');
142
-
143
- });
144
-
145
- }
146
-
147
-
148
-
149
- /**
150
-
151
- * Reverse the migrations.
152
-
153
- *
154
-
155
- * @return void
156
-
157
- */
158
-
159
- public function down()
160
-
161
- {
162
-
163
- Schema::dropIfExists('comments');
164
-
165
- }
166
-
167
- }
168
-
169
- ```
170
-
171
-
172
-
173
- # models
174
-
175
-
176
-
177
- ```php
178
-
179
- <?php
180
-
181
-
182
-
183
- namespace App;
184
-
185
-
186
-
187
- use Illuminate\Database\Eloquent\Model;
188
-
189
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
190
-
191
- use Illuminate\Database\Eloquent\Relations\HasMany;
192
-
193
-
194
-
195
- class Recipe extends Model
196
-
197
- {
198
-
199
- protected $fillable = [
200
-
201
- 'name',
202
-
203
- 'createdBy'
204
-
205
- ];
206
-
207
-
208
-
209
- /**
210
-
211
- * @return HasMany
212
-
213
- */
214
-
215
- public function comments()
216
-
217
- {
218
-
219
- return $this->hasMany(Comment::class);
220
-
221
- }
222
-
223
-
224
-
225
- /**
226
-
227
- * @return BelongsTo
228
-
229
- */
230
-
231
- public function createdBy()
232
-
233
- {
234
-
235
- return $this->belongsTo(User::class, 'created_by');
236
-
237
- }
238
-
239
- }
240
-
241
- ```
242
-
243
-
244
-
245
- ```php
246
-
247
- <?php
248
-
249
-
250
-
251
- namespace App;
252
-
253
-
254
-
255
- use Illuminate\Database\Eloquent\Model;
256
-
257
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
258
-
259
-
260
-
261
- class Comment extends Model
262
-
263
- {
264
-
265
- protected $fillable = [
266
-
267
- 'content',
268
-
269
- 'createdBy'
270
-
271
- ];
272
-
273
-
274
-
275
- /**
276
-
277
- * @return BelongsTo
278
-
279
- */
280
-
281
- public function recipe()
282
-
283
- {
284
-
285
- return $this->belongsTo(Recipe::class);
286
-
287
- }
288
-
289
-
290
-
291
- /**
292
-
293
- * @return BelongsTo
294
-
295
- */
296
-
297
- public function createdBy()
298
-
299
- {
300
-
301
- return $this->belongsTo(User::class, 'created_by');
302
-
303
- }
304
-
305
- }
306
-
307
- ```
308
-
309
-
310
-
311
- # controllers
312
-
313
-
314
-
315
- ```php
316
-
317
- <?php
318
-
319
-
320
-
321
- namespace App\Http\Controllers;
322
-
323
-
324
-
325
- use App\Recipe;
326
-
327
- use Illuminate\View\View;
328
-
329
-
330
-
331
- class RecipeController extends Controller
332
-
333
- {
334
-
335
- /**
336
-
337
- * @return View
338
-
339
- */
340
-
341
- public function index()
342
-
343
- {
344
-
345
- $recipes = Recipe::query()
346
-
347
- ->with(['comments', 'createdBy'])
348
-
349
- ->paginate();
350
-
351
- return view('recipes.index', compact('recipes'));
352
-
353
- }
354
-
355
-
356
-
357
- /**
358
-
359
- * @param Recipe $recipe
360
-
361
- * @return View
362
-
363
- */
364
-
365
- public function show(Recipe $recipe)
366
-
367
- {
368
-
369
- return view('recipes.show', compact('recipe'));
370
-
371
- }
372
-
373
- }
374
-
375
- ```
376
-
377
-
378
-
379
- ```php
380
-
381
- <?php
382
-
383
-
384
-
385
- namespace App\Http\Controllers;
386
-
387
-
388
-
389
- use App\Comment;
390
-
391
- use App\Recipe;
392
-
393
- use Illuminate\Http\RedirectResponse;
394
-
395
- use Illuminate\Http\Request;
396
-
397
- use Illuminate\Support\Facades\Auth;
398
-
399
-
400
-
401
- class CommentController extends Controller
402
-
403
- {
404
-
405
- /**
406
-
407
- * @param Request $request
408
-
409
- * @param Recipe $recipe
410
-
411
- * @return RedirectResponse
412
-
413
- */
414
-
415
- public function store(Request $request, Recipe $recipe)
416
-
417
- {
418
-
419
- $recipe->comments()->save(new Comment([
420
-
421
- 'content' => $request->get('content'),
422
-
423
- 'created_by' => Auth::id()
424
-
425
- ]));
426
-
427
-
428
-
429
- return redirect()->route('recipes', compact('recipe'));
430
-
431
- }
432
-
433
- }
434
-
435
- ```
436
-
437
-
438
-
439
- # routing
440
-
441
-
442
-
443
- ```php
444
-
445
- Route::resource('recipes', 'RecipeController');
446
-
447
- Route::post('comments/{recipe}', 'CommentController@store')->name('comments.store');
448
-
449
- ```
450
-
451
-
452
-
453
- # blade
454
-
455
-
456
-
457
- ```php
458
-
459
- @section('content')
460
-
461
- <h1>レシピID:{{ $recipe->id }}の詳細ページ</h1>
462
-
463
-
464
-
465
- @auth()
466
-
467
- {!! Form::open(['route' => 'comments.store']) !!}
468
-
469
- <div class="form-group">
470
-
471
- {!! Form::textarea('content',['class' => 'form-control']) !!}
472
-
473
- {!! Form::submit('投稿', ['class' => 'btn btn-primary btn-block']) !!}
474
-
475
- </div>
476
-
477
- {!! Form::close() !!}
478
-
479
- @endauth
480
-
481
-
482
-
483
- @foreach($recipe->comments as $comment)
484
-
485
- <p>{{ $comment->content }}</p>
486
-
487
- @endforeach
488
-
489
-
490
-
491
- @endsection
492
-
493
- ```

1

追記

2020/01/08 07:46

投稿

退会済みユーザー
test CHANGED
@@ -447,3 +447,47 @@
447
447
  Route::post('comments/{recipe}', 'CommentController@store')->name('comments.store');
448
448
 
449
449
  ```
450
+
451
+
452
+
453
+ # blade
454
+
455
+
456
+
457
+ ```php
458
+
459
+ @section('content')
460
+
461
+ <h1>レシピID:{{ $recipe->id }}の詳細ページ</h1>
462
+
463
+
464
+
465
+ @auth()
466
+
467
+ {!! Form::open(['route' => 'comments.store']) !!}
468
+
469
+ <div class="form-group">
470
+
471
+ {!! Form::textarea('content',['class' => 'form-control']) !!}
472
+
473
+ {!! Form::submit('投稿', ['class' => 'btn btn-primary btn-block']) !!}
474
+
475
+ </div>
476
+
477
+ {!! Form::close() !!}
478
+
479
+ @endauth
480
+
481
+
482
+
483
+ @foreach($recipe->comments as $comment)
484
+
485
+ <p>{{ $comment->content }}</p>
486
+
487
+ @endforeach
488
+
489
+
490
+
491
+ @endsection
492
+
493
+ ```