Lucas Ting
Lucas Ting

Reputation: 9

Laravel 5.2 Update Record

I'm doing a simple CRUD. I have problem with editing and updating the record in the database.

This is my route.php:

Route::post('/task/edit',function (Request $request, $id){
$task = Task::find($id);
$task->name = Input::get('name');
$task->save();

return redirect('/');
});

This is my form:

<form action="{{url('/task/edit')}}" method="POST" role="form">
  {{csrf_field()}}
  <label for="editTask" class="control-label">Edit Task</label>
  <input type="text" class="form-control" name="name">
  <br>
  <button class="btn btn-success form-control">Submit</button>
</form>

Can I know how to fix this? I was referring to Basic Task List and wanted to add one more function which is update. Or did I just do something completely wrong?

Upvotes: 0

Views: 619

Answers (1)

Calin Blaga
Calin Blaga

Reputation: 1373

It should be:

Route::post('/task/edit', function (Request $request){

    $task = Task::find($request->input('id'));
    $task->name = $request->input('name');
    $task->save();

    return redirect('/');
});

and in the form you must add an input type hidden

<input type="hidden" name="id" value="{{ $id }}" />

and don't forget to place those routes inside the

Route::group(['middleware' => ['web']], function () {...});

in order for _token to work

Upvotes: 1

Related Questions