Search code examples
laravelmodelreplicationlaravel-5.3relationships

Replicate table, to 3 other tables, while updating the trip_id column - Laravel 5.3


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:

Trips Model

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:

What To Bring On Trip Table

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.


Solution

  • 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();
    
    }