Laravel remove item from list of tables / db
I am trying to remove an item from a generated table of items that are from a database table.
My route:
Route::delete('destroy/{deviceID}', ['as' => 'destroyDevice', 'uses' => 'DeviceController@destroyDevice']);
My controller for deleting an element:
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 clip 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 responds, which I am doing wrong.
If I click on the third delete button, the form has this:
<form action="http://localhost/app/public/device/destroy/3" method="post" name="delete_device"></form>
+3
source to share
3 answers
This can be solved by placing the form inside the td tag in this 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 is being ignored for some reason due to invalidation, but I'm not 100% sure. Let people edit this answer;)
+1
source to share