2017-08-08 116 views
0

我將首先解釋表和代碼是如何工作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 –

回答

1

以下代碼將首先獲取與客戶端相關的所有項目。然後將通過循環刪除客戶端的所有項目。

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謝謝,我已經更新了我的代碼。 –

2

希望這可以幫助你

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它的工作;) –

1

我想你可以試試這個:

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

作品!謝謝:) –