Reputation: 4102
I have a view in which I show all products from my database, in that view I can view edit and delete each product.
The delete function does not work properly right now:
View looks like this:
@foreach($products as $productKey => $productValue)
<tr>
<td>{{ $productValue->id }}</td>
<td>{{ $productValue->title }}</td>
<td>
@if($productValue->dr)
<a href="{{ route('editProduct', $productValue->id) }}" title="edit product"></a>
<form action="{{ route('destroyProduct') }}" method="post" name="delete_product_form">
<input type="hidden" name="_method" value="delete">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="id" value="{{ $productValue->id }}">
<a href="#" title="delete" data-form="delete_product_form" data-action="submit"></a>
</form>
...
@endif
</td>
</tr>
@endforeach
And my destroy method looks like this:
public function destroyProduct(Request $request)
{
$productID = $request->get('id');
$product = Product::find($productID);
$deleteFolder = "folderpath...";
if(is_dir($deleteFolder))
{
if(Helper::removeDirRecursive($deleteFolder))
{
if($product)
{
$product->delete();
return redirect()->route('indexProduct')->with('message', 'Success');
}
else
{
return redirect()->route('indexProduct')->with('message', 'Error');
}
}
}
elseif($product)
{
$product->delete();
return redirect()->route('indexProduct')->with('message', 'Success');
}
return redirect()->route('indexProduct')->with('message', 'Error');
}
At the moment it just deletes the newest product, if I echo
$product
I always get the newest product.
How do I have to change my function to delete the product I choose correctly.
EDIT:
I tried this but still did not work:
<form action="{{ route('destroyProduct', $productValue->id) }}" method="post" name="delete_product_form">
EDIT:
MY javascript submit function:
observerSubmitButton: function() {
$('.observeSubmit').on('click', function() {
action = $(this).data('action');
formName = $(this).data('form');
if(action == 'submit')
{
$('form[name="'+formName+'"]').submit();
}
});
},
Upvotes: 0
Views: 4556
Reputation: 48
use
$product=Product::find($id)
or
$product=Product::where('id',$id)
to find the product
then
$product->delete();
to delete it
Upvotes: 0
Reputation: 677
change this:
@if($productValue->dr)
<a href="{{ route('editProduct', $productValue->id) }}" title="edit product"></a>
<form action="{{ route('destroyProduct') }}" method="post" name="delete_product_form">
<input type="hidden" name="_method" value="delete">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="id" value="{{ $productValue->id }}">
<a href="#" title="delete" data-form="delete_product_form" data-action="submit"></a>
</form>
...
@endif
to this
@if($productValue->dr)
<a href="{{ route('editProduct', $productValue->id) }}" title="edit product"></a>
<form action="{{ route('destroyProduct') }}" method="post">
<input type="hidden" name="_method" value="delete">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="id" value="{{ $productValue->id }}">
<button type="submit">Delete</button>
</form>
...
@endif
Or you can have a counter
<?php $count=0; ?>
@foreach($products as $productKey => $productValue)
<tr>
<td>{{ $productValue->id }}</td>
<td>{{ $productValue->title }}</td>
<td>
@if($productValue->dr)
<a href="{{ route('editProduct', $productValue->id) }}" title="edit product"></a>
<form action="{{ route('destroyProduct') }}" method="post" name="delete_product_form{{$count}}">
<input type="hidden" name="_method" value="delete">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="id" value="{{ $productValue->id }}">
<a href="#" title="delete" data-form="delete_product_form{{$count++}}" data-action="submit"></a>
</form>
...
@endif
</td>
</tr>
@endforeach
Upvotes: 2