3

I am trying to delete item from a generated table of items which are from a database table.

My Route:

Route::delete('destroy/{deviceID}', ['as' => 'destroyDevice', 'uses' => 'DeviceController@destroyDevice']);

My Controller method to delete an item:

public function destroyDevice(Request $request, $deviceId = 0)
{
    $device = Device::find($deviceId);

    if($device)
    {
        $device->delete();
        return redirect()->route('index')->with('success', 'Erfolgreich gelöscht');
    }
    else
    {
        return redirect()->route('index')->with('error', 'Fehler');
    }
}

And my blade template:

                <form action="{{ route('destroyDevice', $deviceValue->id) }}" method="post" name="delete_device">
                    <input type="hidden" name="_method" value="delete">
                    <input type="hidden" name="_token" value="{{ csrf_token() }}">
                    <input type="hidden" name="id" value="{{ $deviceValue->id }}">
                    <td>
                        <button type="submit" class="btn btn-danger" name="destroy_device">
                            <span class="glyphicon glyphicon-trash"></span>
                        </button>
                    </td>
                </form>

If I click on the button nothing happens no error no Response, what am I doing wrong.

If I click on the third delete button the form holds this:

<form action="http://localhost/app/public/device/destroy/3" method="post" name="delete_device"></form>
6
  • 1
    When you click on the button, do you see a request send to the server in the web console ? Commented May 11, 2017 at 13:29
  • The console does not Show anything if I click on the button Commented May 11, 2017 at 13:30
  • @EkinOf I posted the whole blade Commented May 11, 2017 at 13:46
  • Could you post your route binding for {deviceID}? Commented May 11, 2017 at 13:53
  • @MarijkeLuttekes Hmm what excatly do you mean, I posted the Action URL if that helps Commented May 11, 2017 at 13:58

3 Answers 3

1

You can solve this by putting the form inside a td tag in that table.

Like this:

  <td> <!--  <--- put these -->
      <form action="{{ route('destroyDevice', $deviceValue->id) }}" method="post" name="delete_device">
          <input type="hidden" name="_method" value="delete">
          <input type="hidden" name="_token" value="{{ csrf_token() }}">
          <input type="hidden" name="id" value="{{ $deviceValue->id }}">

              <button type="submit" class="btn btn-danger" name="destroy_device">
                  <span class="glyphicon glyphicon-trash"></span>
              </button>

      </form>
  </td> <!--  <--- put these -->

I think the form gets ignored somehow due to not being valid, but I am not 100% sure. May people edit this answer ;)

Sign up to request clarification or add additional context in comments.

Comments

0

The parameter is case sensitive so it should be deviceID instead of deviceId

public function destroyDevice(Request $request, $deviceID = 0)

1 Comment

try public function destroyDevice(Request $request, $deviceId)
0

Maybe you have some script that prevents the form to submit, some prevent default maybe on button click or on form submit. Check that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.