Laravel:控制器功能删除数据

Laravel:控制器功能删除数据

问题描述:

我将首先解释表和代码是如何工作Laravel:控制器功能删除数据

我:

  • 项目字段:ID,蛞蝓,为了,公共,路径标头和路径家园。
  • project_translations带字段:id,locale,project_id,标题和标题。
  • 有字段的客户端: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'); 
    } 
+0

因此,问题是客户端被成功删除,但在'client_project'和'project_transalation'记录不会被删除? –

+0

你尝试过'$ cliente-> projects() - > delete();'? – MisaGH

+0

有了这段代码,我刚刚删除了客户端和client_project关系。我需要删除项目和project_translation @SagarGautam –

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

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

嗨@MuhammadInaamMunir我喜欢你这样做!感谢队友 –

+0

试着编辑你的代码,project_translations不是project_translation,谢谢:) –

+0

@LluísPuigFerrer谢谢,我已经更新了我的代码。 –

希望这可以帮助你

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

也许更好的办法是使用foreing键

+0

Gracias pablo!谢谢巴勃罗! @PabloBarbieFumarola它的工作;) –

我想你可以试试这个:

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

希望为你工作!!!

+0

作品!谢谢:) –