Lucas Ting Lucas Ting - 1 year ago 164
PHP Question

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');

return redirect('/');

This is my form:

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

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?

Answer Source

It should be:

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

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

    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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download