dra87370 2018-06-09 08:08 采纳率: 100%
浏览 72
已采纳

Laravel destroy函数总是删除id最低的记录

im new to laravel. So im trying to delete a record from database from my index page. But it only delete the record with the lowest primary key value, my id field. Here's my controller code for destroy function:

public function destroy(Province $province)
    {
        //

        $findProvince = DB::table('provinces')->where('id', $province->id)->delete();

        if($findProvince){
            return redirect()->route('provinces.index')->with('success', 'Province deleted successfully');
        }
        return back()->with('error', 'Province could not be deleted');

        //var_dump($province->id);
    }

Here's my view code:

@extends('layouts.app')

@section('content')
<div class="col-md-8 offset-md-2">
    <br/>
    <div>
        <a href="provinces/create" class="btn btn-primary">Add New Province</a>
    </div>
    <br/>
    <table class="table">
        <thead>
            <tr>
            <th scope="col">#</th>
            <th scope="col">Name</th>
            <th scope="col">Image</th>
            <th scope="col">Operations</th>
            </tr>
        </thead>
        <tbody>
            @foreach($provinces as $province)
                <tr>
                    <th scope="row">{{ $province->id }}</th>
                    <td>{{ $province->name }}</td>
                    <td><img src="../{{ $province->imgPath }}" width="200px"/></td>
                    <td>
                        <a href="provinces/{{ $province->id }}/edit" class="btn btn-warning">Edit</a> | 
                        <a
                                class="btn btn-danger"
                                href="#"
                                onclick="
                                  var result = confirm('Are you sure you wish to delete this province?');
                                      if( result ){
                                              event.preventDefault();
                                              document.getElementById('delete-form').submit();
                                      }
                                ">Delete
                        </a>

                        <form id="delete-form" action="{{route('provinces.destroy', $province->id)}}" method="POST" style="display: none;">
                            <input type="hidden" name="_method" value="delete">
                            {{ csrf_field() }}
                        </form>
                    </td>
                </tr>
            @endforeach

        </tbody>
    </table>
</div>
@endsection

I tried using var_dump on the $province->id but it only output the lowest 'id' value.

  • 写回答

2条回答 默认 最新

  • doushi8187 2018-06-15 04:33
    关注

    Thanks everyone for your help. And sorry that it took me long to reply. I've been really busy with other schools. So i managed to track down the problem with my code. It's with my view. All the created delete form has the same id. So if i just added the record id to the form. It works just fine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记