This is my code :
Route
Route::resource('ItemName', 'ItemNameController');
Controller
public function destroy(ItemName $itemName)
{
$itemName->delete();
return redirect('ItemName')->with('success', 'Item Has Been Delete');
}
View
<form action="{{ route('ItemName', $ItemName->id) }}" method="post">
@csrf
@method("DELETE")
<input type="submit" class="btn btn-danger btn-sm" href="{{ $ItemName-
>id }}" value="Delete" onclick="return confirm('Are You Sure To Delete
This Item? #{{ $ItemName->inc }} ')">
</form>
The problem is that code doesn't work to delete the item in table, any help? Thanks
Try this in your controller
public function destroy($id)
{
ItemName::destroy($id);
return redirect('ItemName')->with('success', 'Item Has Been Delete');
}
or you could try
public function destroy($id)
{
$itemName = ItemName::find($id);
$itemName ->delete();
return redirect('ItemName')->with('success', 'Item Has Been Delete');
}
Change your view code like this
<form action="{{ route('ItemName', $ItemName->id) }}" method="post" onSubmit="return confirm('Are You Sure To Delete
This Item? #{{ $ItemName->inc }} ')">
@csrf
@method("DELETE")
<button class="btn btn-danger btn-sm">Delete</button>
</form>
Or also you can set action like this action="url('/ItemName/'. $ItemName->id)"
try this so in my blade is
<a href="{{url('path.'/'.$row->id.'/soft-delete')}}" class="btn btn-sm btn-danger"> <i class="fa fa-trash-o"></i> </a>
and then controller is
public function soft_delete($id)
{
if($this->data->find($id)->delete()) {
return redirect()->back()->with('success', 'Your data has been moved to trash');
}else {
return redirect()->back()->with('error', 'Your data has not been moved to trash.');
}
}
and my routes is
Route::get('path/{id}/soft-delete', 'EventController@soft_delete');
if you want softdelete use softdeletes else no need to use that
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.