質問編集履歴

2

TaskControllerファイルの追記

2020/09/25 01:51

投稿

ChickBeans
ChickBeans

スコア1

test CHANGED
File without changes
test CHANGED
@@ -176,6 +176,10 @@
176
176
 
177
177
  ```php
178
178
 
179
+ <!-- views/tasks/index.blade.php -->
180
+
181
+
182
+
179
183
  @extends('layout')
180
184
 
181
185
 
@@ -295,3 +299,181 @@
295
299
  @endsection
296
300
 
297
301
  ```
302
+
303
+
304
+
305
+ ```php
306
+
307
+ <?php
308
+
309
+
310
+
311
+ namespace App\Http\Controllers;
312
+
313
+
314
+
315
+ use App\Http\Requests\CreateTask;
316
+
317
+ use App\Http\Requests\EditTask;
318
+
319
+ use App\Models\Folder;
320
+
321
+ use App\Models\Task;
322
+
323
+ use Illuminate\Http\Request;
324
+
325
+
326
+
327
+ class TaskController extends Controller
328
+
329
+ {
330
+
331
+ public function index(int $id)
332
+
333
+ {
334
+
335
+ // Folderテーブルから全データを取得
336
+
337
+ $folders = Folder::all();
338
+
339
+
340
+
341
+ // Folderテーブルから選ばれたフォルダを取得する
342
+
343
+ $current_folder = Folder::find($id);
344
+
345
+
346
+
347
+ // 選ばれたフォルダに紐づくタスクを取得する
348
+
349
+ // $tasks = Task::where('folder_id', $current_folder->id)->get();
350
+
351
+ $tasks = $current_folder->tasks()->get();
352
+
353
+
354
+
355
+ return view('tasks/index', [
356
+
357
+ 'folders' => $folders,
358
+
359
+ 'current_folder_id' => $current_folder->id,
360
+
361
+ 'tasks' => $tasks,
362
+
363
+ ]);
364
+
365
+ }
366
+
367
+
368
+
369
+ /**
370
+
371
+ * GET /folders/{id}/tasks/create
372
+
373
+ */
374
+
375
+ public function showCreateForm(int $id)
376
+
377
+ {
378
+
379
+ return view('tasks/create', [
380
+
381
+ 'folder_id' => $id,
382
+
383
+ ]);
384
+
385
+ }
386
+
387
+
388
+
389
+ public function create(int $id, CreateTask $request)
390
+
391
+ {
392
+
393
+ $current_folder = Folder::find($id);
394
+
395
+
396
+
397
+ $task = new Task();
398
+
399
+ $task->title = $request->title;
400
+
401
+ $task->due_date = $request->due_date;
402
+
403
+
404
+
405
+ $current_folder->tasks()->save($task);
406
+
407
+
408
+
409
+ return redirect()->route('tasks.index', [
410
+
411
+ 'id' => $current_folder->id,
412
+
413
+ ]);
414
+
415
+ }
416
+
417
+
418
+
419
+ /**
420
+
421
+ * GET /folders/{id}/tasks/{task_id}/edit
422
+
423
+ */
424
+
425
+ public function showEditForm(int $id, int $task_id)
426
+
427
+ {
428
+
429
+ $task = Task::find($task_id);
430
+
431
+
432
+
433
+ return view('tasks/edit', [
434
+
435
+ 'task' => $task,
436
+
437
+ ]);
438
+
439
+ }
440
+
441
+
442
+
443
+ public function edit(int $id, int $task_id, EditTask $request)
444
+
445
+ {
446
+
447
+ // 1 過去に登録されている情報を取得
448
+
449
+ $task = Task::find($task_id);
450
+
451
+
452
+
453
+ // 2
454
+
455
+ $task->title = $request->title;
456
+
457
+ $task->status = $request->status;
458
+
459
+ $task->due_date = $request->due_date;
460
+
461
+ $task->save();
462
+
463
+
464
+
465
+ // 3
466
+
467
+ return redirect()->route('tasks.index', [
468
+
469
+ 'id' => $task->folder_id,
470
+
471
+ ]);
472
+
473
+ }
474
+
475
+ }
476
+
477
+
478
+
479
+ ```

1

bladeの追加

2020/09/25 01:51

投稿

ChickBeans
ChickBeans

スコア1

test CHANGED
File without changes
test CHANGED
@@ -30,6 +30,8 @@
30
30
 
31
31
 
32
32
 
33
+ **models**
34
+
33
35
  ```php
34
36
 
35
37
  <?php
@@ -167,3 +169,129 @@
167
169
 
168
170
 
169
171
  ```
172
+
173
+
174
+
175
+ **blade**
176
+
177
+ ```php
178
+
179
+ @extends('layout')
180
+
181
+
182
+
183
+ @section('content')
184
+
185
+ <div class="container">
186
+
187
+ <div class="row">
188
+
189
+ <div class="col col-md-4">
190
+
191
+ <nav class="panel panel-default">
192
+
193
+ <div class="panel-heading">フォルダ</div>
194
+
195
+ <div class="panel-body">
196
+
197
+ <a href="{{ route('folders.create') }}" class="btn btn-default btn-block">
198
+
199
+ フォルダを追加する
200
+
201
+ </a>
202
+
203
+ </div>
204
+
205
+ <div class="list-group">
206
+
207
+ @foreach($folders as $folder)
208
+
209
+ <a href="{{ route('tasks.index', ['id' => $folder->id]) }}" class="list-group-item {{ $current_folder_id === $folder->id ? 'active' : '' }}">
210
+
211
+ {{ $folder->title }}
212
+
213
+ </a>
214
+
215
+ @endforeach
216
+
217
+ </div>
218
+
219
+ </nav>
220
+
221
+ </div>
222
+
223
+ <div class="column col-md-8">
224
+
225
+ <div class="panel panel-default">
226
+
227
+ <div class="panel-heading">タスク</div>
228
+
229
+ <div class="panel-body">
230
+
231
+ <div class="text-right">
232
+
233
+ <a href="{{ route('tasks.create', ['id' => $current_folder_id]) }}" class="btn btn-default btn-block">
234
+
235
+ タスクを追加する
236
+
237
+ </a>
238
+
239
+ </div>
240
+
241
+ </div>
242
+
243
+ <table class="table">
244
+
245
+ <thead>
246
+
247
+ <tr>
248
+
249
+ <th>タイトル</th>
250
+
251
+ <th>状態</th>
252
+
253
+ <th>期限</th>
254
+
255
+ <th></th>
256
+
257
+ </tr>
258
+
259
+ </thead>
260
+
261
+ <tbody>
262
+
263
+ @foreach($tasks as $task)
264
+
265
+ <tr>
266
+
267
+ <td>{{ $task->title }}</td>
268
+
269
+ <td>
270
+
271
+ <span class="label {{ $task->status_class }}">{{ $task->status_label }}</span>
272
+
273
+ </td>
274
+
275
+ <td>{{ $task->formatted_due_date }}</td>
276
+
277
+ <td><a href="{{ route('tasks.edit', ['id' => $task->folder_id, 'task_id' => $task->id]) }}">編集</a></td>
278
+
279
+ </tr>
280
+
281
+ @endforeach
282
+
283
+ </tbody>
284
+
285
+ </table>
286
+
287
+ </div>
288
+
289
+ </div>
290
+
291
+ </div>
292
+
293
+ </div>
294
+
295
+ @endsection
296
+
297
+ ```