【问题标题】:Laravel: Controller function to delete the dataLaravel:删除数据的控制器功能
【发布时间】:2018-01-15 22:42:34
【问题描述】:

我将首先解释表格和代码的工作原理

表格

我有:

  • 具有以下字段的项目:id、slug、order、public、pathheader 和 pathhome。
  • project_translations 包含以下字段:id、locale、project_id、title 和 caption。
  • 具有以下字段的客户端:id、名称、slug 和优先级。
  • client_project 包含字段:id、client_id 和 project_id

代码的工作原理

当我创建一个项目时,我也会创建两个项目翻译(每个语言环境一个,例如:'es'、'en')。然后我选择一个客户,这将建立关系 client_project。

当我删除项目时,我同时删除了具有相同project_id的project_translations和project_id相同的client_project行。

我想要什么

当我删除客户端时,删除字段client_id具有相同值的行(这是有效的),然后删除与我删除的客户端有关系的项目的项目和projects_translations。

我的函数目前的样子

public function destroyClient($id) //Destruir cliente y todo lo relacionado de la bbdd
    {
        $cliente = Client::find($id); 
        $cliente->delete(); //delete the client
        DB::table('client_project')->where('client_id',$id)->delete(); //delete the client_project relations which field client_id is the same that the client i just deleted.

        return redirect()->route('admin.clients');
    }

【问题讨论】:

  • 那么,问题是客户端被成功删除但client_projectproject_transalation中的记录没有被删除?
  • 你试过$cliente->projects()->delete();吗?
  • 暂时用这段代码我只是删除了客户和客户项目的关系。我需要删除项目和 project_translation @SagarGautam
  • 你需要优化数据库。建立正确的关系和级联将更容易删除多个表上的记录
  • 是的@MisaGH 并且不工作。

标签: php mysql sql laravel


【解决方案1】:

希望对你有帮助

public function destroyClient($id) //Destruir cliente y todo lo relacionado de la bbdd
    {
        $cliente = Client::find($id);
        $cliente_project = DB::table('client_project')->where('client_id', $id)->first();
        $project_id = $cliente_project->project_id;
        $cliente->delete(); //delete the client
        DB::table('client_project')->where('client_id',$id)->delete(); //delete the client_project relations which field client_id is the same that the client i just deleted.

        DB::table('projects')->where('id',$project_id)->delete();
        DB::table('project_translations')->where('project_id',$project_id)->delete();

        return redirect()->route('admin.clients');
    }

也许更好的方法是使用外键

【讨论】:

  • 感谢巴勃罗!谢谢巴勃罗! @PabloBarbieFumarola 它正在工作;)
【解决方案2】:

我想你可以试试这个:

public function destroyClient($id) //Destruir cliente y todo lo relacionado de la bbdd
{
    $cliente = Client::find($id); 
    $cliente->delete(); //delete the client
    $project = DB::table('client_project')->where('client_id',$id)->first();
    DB::table('client_project')->where('client_id',$id)->delete(); 


    DB::table('projects')->where('id',$project->project_id)->delete();

    DB::table('project_translations')->where('project_id',$project->project_id)->delete(); 


    return redirect()->route('admin.clients');
}

希望这对你有用!!!

【讨论】:

  • 有效!谢谢:)
【解决方案3】:

以下代码将首先获取与客户端相关的所有项目。然后会循环删除一个客户端的所有项目。

public function destroyClient($id) 
{
    $cliente = Client::find($id); 
    $cliente->delete(); //delete the client

    // get list of all projects of client
    $projects = DB::table('client_project')->where('client_id',$id)->get();

    DB::table('client_project')->where('client_id',$id)->delete(); 

    // delete all projects of client
    foreach($projects as $project)
    {
        DB::table('projects')->where('id',$project->id)->delete();

        DB::table('project_translations')->where('project_id',$project->id)->delete(); 
    }    

    return redirect()->route('admin.clients');
}

【讨论】:

  • 嗨@MuhammadInaamMunir 我喜欢你这样做的方式!谢谢老哥
  • 尝试编辑您的代码,project_translations 不是 project_translation,谢谢:)
猜你喜欢
  • 2016-02-04
  • 2013-10-11
  • 2016-11-01
  • 2019-10-21
  • 2021-01-04
  • 2021-09-06
  • 2017-03-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多