【问题标题】:Laravel delete method on products not workingLaravel删除产品不起作用的方法
【发布时间】:2016-11-22 06:41:18
【问题描述】:

我有一个视图,其中显示了我的数据库中的所有产品,在该视图中我可以查看编辑和删除每个产品。

删除功能现在不能正常工作:

视图如下所示:

@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

我的销毁方法如下所示:

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');
}

目前它只是删除最新的产品,如果我回应的话

$product

我总是得到最新的产品。

我必须如何更改我的功能才能删除我正确选择的产品。

编辑:

我试过了,但还是不行:

<form action="{{ route('destroyProduct', $productValue->id) }}" method="post" name="delete_product_form">

编辑:

我的javascript提交函数:

observerSubmitButton: function() {
    $('.observeSubmit').on('click', function() {
        action   = $(this).data('action');
        formName = $(this).data('form');

        if(action == 'submit')
        {
            $('form[name="'+formName+'"]').submit();
        }

    });
},

【问题讨论】:

  • 如果您在创建它的行之后记录 productID 会返回什么?
  • 最新id的字符串"55"
  • 哦。 nvm我误会了,我以为是获取id的问题,但是你想要的id不应该是55对吗?
  • "55",一个字符串,没有数组,没有int
  • no 我总是抓取最新的产品id 55,这就是问题所在,我想根据哪个盒子(标签)删除当前的id点击

标签: php laravel blade destroy


【解决方案1】:

改变这个:

@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

到这里

@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

或者你可以有一个柜台

<?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

【讨论】:

  • 谢谢,但是不行,还是删除最后一个条目
  • 我认为传递给控制器​​的 id 是最后一个条目的 id,做一些事情而不是 redirect() 做 return response()-&gt;json($productID); 并检查 id 是否与您在表中单击的 id 相同/形式。
  • dd($productID) 一直是最后一个
  • 是的,它总是最后一个条目(产品)
  • 发现问题是您的表单名称,因为您有多个具有相同名称的表单,最后一个表单会覆盖其他表单,因此解决方案制作一个按钮删除&lt;button type="submit"&gt;Delete&lt;/button&gt;它将提交您当前的表格,您最好删除您的表格名称。或者您可以有一个计数器和您的表单名称的计数器,以使每个表单具有唯一的名称。
【解决方案2】:

使用

$product=Product::find($id) 

 $product=Product::where('id',$id)

寻找产品
那么

 $product->delete(); 

删除它

【讨论】:

  • 我必须添加什么吗?因为它不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-18
  • 1970-01-01
  • 2018-10-18
相关资源
最近更新 更多