This is sort of a tricky question. Ill try to explain it as best as I can. I have a trip, and I want users to click a copy button on that trip, which will just replicate that trip again, so users don't have to fill out a huge form again.
The trip form saves to 4 models (tables).
This is how Im doing the replicating right now:
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();
}
So this is how a replicated trip looks like on the main trips table:
Then I got 3 other diffrent models (tables) which it saves data to from that trip ID.
So for example here is my other table that the data is replicated to:
As you can see, the data replicated succesfully, BUT, the "trip_id" column, which in this case is 1259 is still the same as the original trip I replicated. I need to get the new replicated trip_id, and insert it into the other 3 models I have. So in this case, the second row would be 1262 NOT 1259.
You can try it as:
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();
}