Reputation: 747
I am using laravel 8 for an edit operation for which I have created Resource controller by using php artisan command and create has worked perfectly but edit operation is not working correctly giving error as 'Missing required parameter for [Route: tasks.update] [URI: tasks/{task}] [Missing parameter: task]'
web.php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\TaskController;
Route::resource('tasks', TaskController::class);
list.blade.php
<table class="table table-bordered">
<thead>
<tr>
<th>Title</th>
<th>Assigned User</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@if(count($tasks)>0)
@foreach($tasks AS $record)
<tr>
<td>{{ $record->title }}</td>
<td>{{ $record->name }}</td>
<td>{{ $record->status }}</td>
<td> <a href="{{ route('tasks.edit', $record->id) }}"> Edit </a> | <a href="{{ route('tasks.delete', $record->id) }}"> Delete </a> </td>
</tr>
@endforeach
@else
<tr><td colspan="3" align="center">No Records Found.</td></tr>
@endif
</tbody>
</table>
edit.blade.php
<form name="" action="{{ route('tasks.update', $record->id) }}" method="POST">
@csrf
@method('PUT')
<div class="form-group">
<label for="title">Title: </label>
<input type="text" name="title" class="form-control" placeholder="Enter Title" value="{{ $record->title }}">
<span class="text-danger">@error('title') {{ $message }} @enderror</span>
</div>
<div class="form-group">
<label for="password">Assign to User: </label>
<select class="form-control" name="assigned_user_id">
<option value="">--Select User--</option>
@foreach($users as $each_user)
<option value="{{ $each_user->id }}" {{ ($each_user->id == $record->assigned_user_id) ? 'selected' : '' }} >{{ $each_user->name }}</option>
@endforeach
</select>
<span class="text-danger">@error('assigned_user_id') {{ $message }} @enderror</span>
</div>
<div class="form-group">
<label for="password">Task Status: </label>
<select class="form-control" name="task_status_id">
<option value="">--Select Status--</option>
@foreach($task_status as $each_status)
<option value="{{ $each_status->id }}" {{ ($each_status->id == $record->task_status_id) ? 'selected' : '' }} >{{ $each_status->status }}</option>
@endforeach
</select>
<span class="text-danger">@error('task_status_id') {{ $message }} @enderror</span>
</div>
<div class="form-group">
<button type="submit" class="btn btn-block btn-primary">Update</button>
</div>
<br>
</form>
TaskController.php
public function edit(Task $id)
{
// echo $id;exit;
$task = Task::find($id);
$users = DB::table('users')->get();
$task_status = DB::table('task_status')->get();
// dd($task);
return view('tasks.edit',['record'=>$task,'users'=>$users, 'task_status'=>$task_status]);
}
public function update(Request $request,Task $id)
{
}
Any help, Thanks.
Upvotes: 0
Views: 12758
Reputation: 151
In your form change this
action="{{ route('tasks.update', $record->id)
to
action="{{ route('tasks.update', ['task' => $record->id])
and an advice:
Laravel automatically resolves Eloquent models defined in routes or controller actions whose type-hinted variable names match a route segment name.
In your code you should change this
public function edit(Task $id)
{
$task = Task::find($id);
$users = DB::table('users')->get();
$task_status = DB::table('task_status')->get();
return view('tasks.edit',['record'=>$task,'users'=>$users, 'task_status'=>$task_status]);
}
to
public function edit(Task $task)
{
//$task = Task::find($id); Task is already loaded (($id ==> $task))
$users = DB::table('users')->get();
$task_status = DB::table('task_status')->get();
return view('tasks.edit',['record'=>$task,'users'=>$users, 'task_status'=>$task_status]);
}
And if you don't want to bind the model, remove "Task" from parameters.
Upvotes: 3