lundi 1 avril 2019

Laravel: Prevent API resource to be modified after the model is updated

I'm implementing a LOG of the events that occurs on the system. In other words, detect when an object is created, and save the data that was created. When an update comes, I need to store the previous object state and the new one. As I'm using API resources with custom data implementations, I'm reusing them to gather all the needed information.

public function update(Request $request, Attendant $attendant)
{
    $attendantData = $request->input('attendant');

    $prevResource = new AttendantResource($attendant);
    $attendant = AttendantService::Update($attendant, $attendantData);

    $resource = new AttendantResource($attendant);

    $this->createLog($prevResource, $resource);

    return $resource;
}

In the previous code, create a new resource before the Attendant is modified. But at the end, $prevResource and $resource have the same data. The information from $prevResource is update toO.

Is there a way to clone the Model? or instead, Is there a way to prevent the reference update from the $prevResource?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire