Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

roulette6's avatar

How to update/delete using forms and RESTful controllers

Hello:

I am creating a simple webapp and I have a table with each row representing the details of each record in the database. The last two cells for each row are an update and delete button. Can anyone help me figure out whether I need to declare some properties in itemsController or use a different syntax in the form I'm using to delete the item?

Route code

Route::resource('items', 'ItemsController');

ItemsController code (relevant methods)

public function update($id)
{
    $item = Item::findOrFail($id);
    $item->description   = Input::get('description');
    $item->unit          = Input::get('unit');
    $item->unit_price    = Input::get('unit_price');
    $item->average_price = Input::get('average_price');
    $item->store_id      = Input::get('store');
    $item->save();

    return Redirect::route('items.index');
}

public function destroy($id)
{
    $item = Item::findOrFail($id);
    $item->delete();

    return Redirect::route('items.index');
}

Form code to delete/update

@foreach($items as $item)
    <tr>
        <td>{{ $item->id }} | {{ $item->description }}</td>
        <td>{{ $item->unit }}</td>
        <td>${{ $item->unit_price }}</td>
        <td>${{ $item->average_price }}</td>
        <td>{{ $item->store_id }}</td>
        <td>
            {{ Form::open(['method' => 'DELETE', 'route' => 'items.destroy', $item->id]) }}
                {{ Form::hidden('id', $item->id) }}
                {{ Form::submit('Delete', ['class' => 'btn btn-danger']) }}
            {{ Form::close() }}
        </td>
    </tr>
@endforeach
0 likes
20 replies
alenabdula's avatar

Should be


Form::open([ 'method' => 'delete', 'route' => [ 'items.destroy', $item->id ] ])
2 likes
sidscorner's avatar

Route name and Id should be inside the route array. And you don't need the hidden field.

{{ Form::open(['method' => 'DELETE', 'route' => ['items.destroy', $item->id]]) }}
    {{ Form::submit('Delete', ['class' => 'btn btn-danger']) }}
{{ Form::close() }}
6 likes
ttson24's avatar

i used above but i can not understand , browser create below html. it is always method as POST but i want method as DELETE. What is happen?

form method="POST" action="http://laravel.dev.admin/admin/menu/8" accept-charset="UTF-8"

input name="_method" type="hidden" value="DELETE"

input name="_token" type="hidden" value="OAVDTSl7mZhBs8RiNtZknyxX2OTfFGjgRmQFTNJB"

input class="btn btn-danger" type="submit" value="Delete"

form

davorminchorov's avatar

Laravel does some magic behind the scenes and uses the hidden value instead of the action's value. That's why you are able to send DELETE (PUT and PATCH) requests. Only GET and POST requests are supported when using forms.

ttson24's avatar

but I use Route::resource(). So, i want to use method as DELETE. and this case, if i use method as POST , i have to error "Failed to load response data".

davorminchorov's avatar

Route::resource generates a Route::delete('menu/{id}', 'MenuController@destroy') route as part of the 7-8 routes that come with it. Run php artisan route:list to see all the routes you have registered.

ttson24's avatar

i use php artisan route:list, i see method DELETE with route is menu.destroy and url is menu/{menu}. but url is similar method show. so i want add method DELETE into route menu.destroy. when user press button delete. in Route.php, do i to must add Route::delete('menu/{id}', 'MenuController@destroy')?

davorminchorov's avatar

No, it is already added. The routes look similar but the HTTP verbs are different and have different actions.

ttson24's avatar

right! but i use method as DELETE similar verbs as DELETE, but on the browser it always as post and this action can not call destroy function.

in this action i create right?

{!! Form::open(['method' => 'DELETE', 'route' => ['admin.menu.destroy', $menus[$i-1]['id']]]) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}

but view in html :

<form method="POST" action="http://laravel.dev.admin/admin/menu/15" accept-charset="UTF-8">
<input name="_method" type="hidden" value="DELETE">
<input name="_token" type="hidden" value="1hox0VLpoJF8FdtFhShunbEsIhTFCz3qAAFUap81">
<input class="btn btn-danger" type="submit" value="Delete">
</form>

in this case, i want to call destroy function. how i do? could you help me!!

ttson24's avatar

i see. but i can not call destroy function.

bobbybouwmann's avatar

Show your code in your controller? You need to delete it with code of course

ttson24's avatar
/**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
         $process = new MenuProcess();
         $process->deleteMenu($id);     
    }

but important , i can not call this function.

shujjahyamin's avatar

In My Articles Controller class I'm using destroy to delete my articles but its not deleting the Article

public function destroy(Article $article, Requests\ArticleValidationRequest $request){

    $article->delete($request->all());
    return redirect('articles')->with([
        'flash-message'=>'Your Task has been Deleted ',
        'flash-message-important'=>true
    ]);;
}
davorminchorov's avatar

What are you validating for that delete action? The Requests\ArticleValidationRequest stops your method execution and you probably don't see the errors.

vudrok's avatar

I had the same issue, in my case it was some 500 error but it was being sent has a CORS issue, after looking into the error-log I found it was an issue related with the code other than a CORS so this was misleading my tries to fix it.

Try to see if the error code is 500 and then check the logs of your web server.

chientinhte's avatar

<form method="post" action="{{asset("items/{item}")}}"> {{method_field('DELETE')}} {{csrf_field()}} <input type="submit" value = "delete"> </form>

Tried and Success.

1 like
Ravi_Prakash_143's avatar

I am getting same issue. I used {!! Form::open(['method' => 'DELETE', 'route' => ['admin.menu.destroy', $menus[$i-1]['id']]]) !!} {!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!} {!! Form::close() !!} but the route is going to get method.

Please help me anybody. Thanks

Please or to participate in this conversation.