Home > Software design >  Nothing any update with put method in Laravel 8
Nothing any update with put method in Laravel 8

Time:09-23

I'm currently creating a simple todo-list in Laravel 8 and want to be able to update the items of the list. My code doesn't get any error but also nothing changes when I put the submit button.

index.blade.php:

<form action="{{ route('index.update', $task->id) }}" method="POST">
   @csrf
   @method('PUT')
   <td><input type="text" name="content" value="{{ $task->content }}" id=""></td>
   <td><input type="submit" value="Update"></td>
</form>

web.php:

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\TaskController;

Route::resource('index', TaskController::class);

TaskController.php:

public function update(Request $request, Task $task)
{
    $request->validate([
        'content' => 'required',
    ]);
    $task->update($request->all());

    return redirect('index');
}

CodePudding user response:

As @rwd said, the problem is with the route definition. Change it to:

Route::resource('tasks', TaskController::class);

Then in the form:

<form action="{{ route('tasks.update', $task->id) }}" method="POST">
  • Related