这是一个棘手的问题。我试着尽可能地解释它。我有一次旅行,我希望用户点击该旅行的复制按钮,这将再次复制该旅行,因此用户不必再次填写一个巨大的表格。
旅行表格保存为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();
}
所以这就是主要旅行表上复制旅行的样子:
然后我得到了另外3个不同的模型(表格),它将数据保存到旅行ID中。
因此,例如,这是我的另一个表,数据被复制到:
正如您所看到的,数据成功复制,但是," trip_id"列,在这种情况下是1259仍然是我复制的原始行程相同。我需要获取新的复制trip_id,并将其插入我拥有的其他3个模型中。所以在这种情况下,第二行将是1262而不是1259.
答案 0 :(得分: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();
}