前提・実現したいこと
Missing required parameterのエラーを解決したい。
入門LaravelチュートリアルTodoアプリ作成(10)を参考にアプリを作成していました。
しかし、2年前の記事でもあるのでlaravel5.8では正常に動くが、8系ではエラーが起きてしまう状況が多々あり、今回もそれに関する内容かなと思っています。
具体的には、以下です。
発生している問題・エラーメッセージ
Illuminate\Routing\Exceptions\UrlGenerationException Missing required parameter for [Route: tasks.index] [URI: folders/{folder}/tasks] [Missing parameter: folder].
該当のソースコード
index.blade.php
laravel
1@extends('layout') 2 3@section('content') 4 <div class="container"> 5 <div class="row"> 6 <div class="col col-md-4"> 7 <nav class="panel panel-default"> 8 <div class="panel-heading">フォルダ</div> 9 <div class="panel-body"> 10 <a href="{{ route('folders.create') }}" class="btn btn-default btn-block"> 11 フォルダを追加する 12 </a> 13 </div> 14 <div class="list-group"> 15 @foreach($folders as $folder) 16 <a 17 href="{{ route('tasks.index', ['folder' => $folder->id]) }}" 18 class="list-group-item {{ $current_folder_id === $folder->id ? 'active' : '' }}" 19 > 20 {{ $folder->title }} 21 </a> 22 @endforeach 23 </div> 24 </nav> 25 </div> 26 <div class="column col-md-8"> 27 <div class="panel panel-default"> 28 <div class="panel-heading">タスク</div> 29 <div class="panel-body"> 30 <div class="text-right"> 31 <a href="{{ route('tasks.create', ['folder' => $current_folder_id]) }}" class="btn btn-default btn-block"> 32 タスクを追加する 33 </a> 34 </div> 35 </div> 36 <table class="table"> 37 <thead> 38 <tr> 39 <th>タイトル</th> 40 <th>状態</th> 41 <th>期限</th> 42 <th></th> 43 </tr> 44 </thead> 45 <tbody> 46 @foreach($tasks as $task) 47 <tr> 48 <td>{{ $task->title }}</td> 49 <td> 50 <span class="label {{ $task->status_class }}">{{ $task->status_label }}</span> 51 </td> 52 <td>{{ $task->formatted_due_date }}</td> 53 <td><a href="{{ route('tasks.edit', ['folder' => $task->folder_id, 'task' => $task->id]) }}">編集</a></td> 54 </tr> 55 @endforeach 56 </tbody> 57 </table> 58 </div> 59 </div> 60 </div> 61 </div> 62@endsection
TaskController.php
<?php namespace App\Http\Controllers; use App\Models\Folder; use App\Models\Task; use Illuminate\Http\Request; use App\Http\Requests\CreateTask; use App\Http\Requests\EditTask; use Illuminate\Support\Facades\Auth; class TaskController extends Controller { public function index(Folder $folder) { $folders = Auth::user()->folders()->get(); $tasks = $folder->tasks()->get(); return view('tasks/index', [ 'folders' => $folders, 'current_folder_id' => $folder->id, 'tasks' => $tasks, ]); } /** * GET /folders/{id}/tasks/create */ public function showCreateForm(Folder $folder) { return view('tasks/create', [ 'folder_id' => $folder->id, ]); } public function create(Folder $folder, CreateTask $request) { $task = new Task(); $task->title = $request->title; $task->due_date = $request->due_date; $folder->tasks()->save($task); return redirect()->route('tasks.index', [ 'id' => $folder->id, ]); } /** * GET /folders/{id}/tasks/{task_id}/edit */ public function showEditForm(Folder $folder, Task $task) { $this->checkRelation($folder, $task); return view('tasks/edit', [ 'task' => $task, ]); } public function edit(Folder $folder, Task $task, EditTask $request) { $this->checkRelation($folder, $task); $task->title = $request->title; $task->status = $request->status; $task->due_date = $request->due_date; $task->save(); return redirect()->route('tasks.index', [ $folder->id ]); } public function checkRelation(Folder $folder, Task $task) { if($folder->id !== $task->folder_id) { abort(404); } } }
web.php
<?php use Illuminate\Support\Facades\Route; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Auth::routes(); Route::group(['middleware' => 'auth'], function() { Route::get('/','App\Http\Controllers\HomeController@index')->name('home'); Route::get('/folders/create', 'App\Http\Controllers\FolderController@showCreateForm')->name('folders.create'); Route::post('/folders/create', 'App\Http\Controllers\FolderController@create'); Route::group(['middleware' => 'can:view,folder'], function() { Route::get('/folders/{folder}/tasks', 'App\Http\Controllers\TaskController@index')->name('tasks.index'); Route::get('/folders/{folder}/tasks/create', 'App\Http\Controllers\TaskController@showCreateForm')->name('tasks.create'); Route::post('/folders/{folder}/tasks/create', 'App\Http\Controllers\TaskController@create'); Route::get('/folders/{folder}/tasks/{task}/edit', 'App\Http\Controllers\TaskController@showEditForm')->name('tasks.edit'); Route::post('/folders/{folder}/tasks/{task}/edit', 'App\Http\Controllers\TaskController@edit'); }); });
試したこと
元々に記事では、index.blade.phpにて
href="{{ route('tasks.index', ['id' => $folder->id]) }}"
だったので、
href="{{ route('tasks.index', ['folder' => $folder->id]) }}"
に変更してみたが、状況は変わらずでした。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2021/05/23 07:56