2016-11-03 58 views
0

這是一個棘手的問題。我儘可能地盡力解釋它。我有一次旅行,我希望用戶點擊該旅行中的複製按鈕,該複本將再次複製該行程,以便用戶不必再次填寫大量表格。在更新trip_id列的同時將表複製到其他3個表 - Laravel 5.3

旅行表格保存到4個模型(表格)。

這是林如何做複製現在:

public function replicateTrip (Request $request, $slug, $id) { 

     $listing = $request->user()->listings()->where('slug', $slug)->first(); 
     $trip = $listing->trips()->where('id', $id)->first(); 


     $replicateTrip = Trip::find($trip->id); 
     $replicatebringToTrip = BringToTrip::where('trip_id', $id)->first(); 
     $replicateIncludedInPriceForTrip = IncludedPriceTrip::where('trip_id', $id)->first(); 
     $replicateNotIncludedInPriceForTrip = NotIncludedPriceTrip::where('trip_id', $id)->first(); 

     $newTask = $replicateTrip->replicate(); 
     $newTask2 = $replicatebringToTrip->replicate(); 
     $newTask3 = $replicateIncludedInPriceForTrip->replicate(); 
     $newTask4 = $replicateNotIncludedInPriceForTrip->replicate(); 

     $newTask->save(); 
     $newTask2->save(); 
     $newTask3->save(); 
     $newTask4->save(); 

     return redirect()->back(); 

    } 

所以這是一個複製的行程怎麼看起來像在主車次表:

Trips Model

然後我得到了另外3個不同的模型(表格),它將數據保存到該行程ID中。

因此,例如,這裏是我的其他表中的數據複製到:

What To Bring On Trip Table

正如你所看到的,成功地複製的數據,但是,在「trip_id」一欄,在這種情況下是1259仍然是我原來旅行的複製品。我需要獲取新複製的trip_id,並將其插入到其他3個模型中。所以在這種情況下,第二排是1262不是1259

回答

1

,您可以嘗試如下:

public function replicateTrip (Request $request, $slug, $id) { 

    $listing = $request->user()->listings()->where('slug', $slug)->first(); 
    $trip = $listing->trips()->where('id', $id)->first(); 


    $replicateTrip = Trip::find($trip->id); 
    $replicatebringToTrip = BringToTrip::where('trip_id', $id)->first(); 
    $replicateIncludedInPriceForTrip = IncludedPriceTrip::where('trip_id', $id)->first(); 
    $replicateNotIncludedInPriceForTrip = NotIncludedPriceTrip::where('trip_id', $id)->first(); 

    $newTask = $replicateTrip->replicate(); 
    $newTask2 = $replicatebringToTrip->replicate(); 
    $newTask3 = $replicateIncludedInPriceForTrip->replicate(); 
    $newTask4 = $replicateNotIncludedInPriceForTrip->replicate(); 

    $newTask->save(); 

    $newTask2->trip_id = $newTask->id; 
    $newTask3->trip_id = $newTask->id; 
    $newTask4->trip_id = $newTask->id; 

    $newTask2->save(); 
    $newTask3->save(); 
    $newTask4->save(); 

    return redirect()->back(); 

} 
+0

太多,聰明的人,感謝洙多! – David

相關問題