dimanche 20 mai 2018

Laravel Delete record in a row is not as expected

I'm using resource controller to delete record in row by passing a collection into the view.

View:

<tbody>
                                @php $count=1; @endphp
                                @forelse ($products as $product)
                                    <tr>
                                        <td></td>
                                        <td></td>
                                        <td></td>
                                        <td></td>
                                        <td><span class="label label-success">Published</span></td>
                                        <td>
                                            <div class="btn-group">
                                                <a href="/products/" class="btn btn-default btn-xs">View</a>
                                                <a href="/account/edit-product-" class="btn btn-warning btn-xs">Edit</a>
                                                <a href="" class="btn btn-danger btn-xs" onclick="event.preventDefault();document.getElementById('delete-product').submit();">Delete</a>

                                                <form id="delete-product" method="POST" action="" style="display: none;">
                                                    @csrf
                                                    @method('DELETE')
                                                </form>
                                            </div>
                                        </td>
                                    </tr>
                                    @php $count++; @endphp
                                @empty
                                    <tr>
                                        <td colspan="6">No products yet.</td>
                                    </tr>
                                @endforelse
                            </tbody>

Controller:

public function products()
    {
        $products = Product::orderBy('created_at', 'desc')->paginate(10);

        return view('vendor.products')->with('products', $products);
    }
public function destroy(Product $product)
    {
        $product->delete();

        return redirect('/account/products')->with('success', 'Product deleted successfully.');
    }

When I click the "Delete" button, it deletes the last post (the first post in database, since it is sorted in descending).

Can someone tells me where it is wrong? I think initally the code work just fine, until I make some other modification and it is "magically" did not work as expected.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2IDuNhq
via IFTTT

Aucun commentaire:

Enregistrer un commentaire