vue.js 如何在Laravel控制器中使用查询从链接到另一个具有主键和外键的表的表中删除一行?

6yoyoihd  于 2022-11-25  发布在  Vue.js
关注(0)|答案(1)|浏览(79)

我有这个这个函数下面使用下面的查询从数据库检索数据:

$matterConveyancing_all_tables = DB::table('matter_conveyancings')
    ->join('matter_conveyancing_b_s', 'matter_conveyancings.id', '=', 'matter_conveyancing_b_s.matter_conveyancings_id')
    ->join('matter_conveyancing_c_s', 'matter_conveyancings.id', '=', 'matter_conveyancing_c_s.matter_conveyancings_id')
    ->join('matter_conveyancing_d_s', 'matter_conveyancings.id', '=', 'matter_conveyancing_d_s.matter_conveyancings_id')
    ->join('matter_conveyancing_e_s', 'matter_conveyancings.id', '=', 'matter_conveyancing_e_s.matter_conveyancings_id')
    ->select('matter_conveyancings.*', 'matter_conveyancing_b_s.*', 'matter_conveyancing_c_s.*', 'matter_conveyancing_d_s.*', 'matter_conveyancing_e_s.*')
    ->get();

把一切都做好
现在我尝试用同样的方法,用下面的代码查询数据库的删除

public function destroy(MatterConveyancing $matterConveyancing,$id)
{
    DB::table('matter_conveyancings')
    ->join('matter_conveyancing_b_s', 'matter_conveyancings.id', '=', 'matter_conveyancing_b_s.'.$id)
    ->join('matter_conveyancing_c_s', 'matter_conveyancings.id', '=', 'matter_conveyancing_c_s.'.$id)
    ->join('matter_conveyancing_d_s', 'matter_conveyancings.id', '=', 'matter_conveyancing_d_s.'.$id)
    ->join('matter_conveyancing_e_s', 'matter_conveyancings.id', '=', 'matter_conveyancing_e_s.'.$id)
    ->delete();
    // $delete->delete();
    return response()->json([
        'message'=>"True!"
    ]);
}

我尝试了上面的方法,但得到了以下错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'matter_conveyancing_b_s.1' in 'on clause' (SQL: delete `matter_conveyancings` from `matter_conveyancings` inner join `matter_conveyancing_b_s` on `matter_conveyancings`.`id` = `matter_conveyancing_b_s`.`1` inner join `matter_conveyancing_c_s` on `matter_conveyancings`.`id` = `matter_conveyancing_c_s`.`1` inner join `matter_conveyancing_d_s` on `matter_conveyancings`.`id` = `matter_conveyancing_d_s`.`1` inner join `matter_conveyancing_e_s` on `matter_conveyancings`.`id` = `matter_conveyancing_e_s`.`1`)

我尝试只返回ID,以检查它是否返回正确的ID,并且它正在返回正确的ID
那么问题出在哪里呢?
“来自检视的额外程式码”

deleteConveyancingMatter(id) {
  if (confirm("Are you sure to delete this Matter ?")) {
    this.axios
      .delete(`/api/auth/matter-conv/${id}`)
      .then((response) => {
        this.getConveyMatters();
      })
      .catch((error) => {
        console.log(error);
      });
  }
},

按钮:

<a style="color: #7367f0" @click="deleteConveyancingMatter(conveymatter.id)"
                  ><i class="fa-solid fa-trash-can"></i
                ></a>

更新:
我尝试了以下方法来添加WHERE,并设法接收到消息(完成!),但没有删除任何数据。

public function destroy(MatterConveyancing $matterConveyancing,$id)
{
    DB::table('matter_conveyancings')
    ->join('matter_conveyancing_b_s', 'matter_conveyancings.id', '=', 'matter_conveyancing_b_s.matter_conveyancings_id')
    ->join('matter_conveyancing_c_s', 'matter_conveyancings.id', '=', 'matter_conveyancing_c_s.matter_conveyancings_id')
    ->join('matter_conveyancing_d_s', 'matter_conveyancings.id', '=', 'matter_conveyancing_d_s.matter_conveyancings_id')
    ->join('matter_conveyancing_e_s', 'matter_conveyancings.id', '=', 'matter_conveyancing_e_s.matter_conveyancings_id')
    ->where('matter_conveyancings.id',$id)
    ->delete('matter_conveyancings', 'matter_conveyancing_b_s', 'matter_conveyancing_c_s','matter_conveyancing_d_s','matter_conveyancing_e_s');
    return response()->json([
        'message'=>'Done!'
    ]);
}
rdrgkggo

rdrgkggo1#

用户“bassxzero”提供的答案

DB::table('matter_conveyancings')
    ->join('matter_conveyancing_b_s', 'matter_conveyancings.id', '=', 'matter_conveyancing_b_s.matter_conveyancings_id')
    ->join('matter_conveyancing_c_s', 'matter_conveyancings.id', '=', 'matter_conveyancing_c_s.matter_conveyancings_id')
    ->join('matter_conveyancing_d_s', 'matter_conveyancings.id', '=', 'matter_conveyancing_d_s.matter_conveyancings_id')
    ->join('matter_conveyancing_e_s', 'matter_conveyancings.id', '=', 'matter_conveyancing_e_s.matter_conveyancings_id')
    ->where('matter_conveyancings.id',$id)
    ->delete();

非常感谢bassxzero

相关问题