mardi 27 octobre 2020

What is best practice to save history in laravel?

We have big laravel application . In many controller method we are calling golbal helper function which is saving history. Here is our method.

            $user_id =  $userId;
            $user_model = $userModel;
            $model = Consignment::class;
            $object_id = $consignment->id;
            $status = getConsignmentStatus($consignment->status);
            $comment = "Consignment has been created";
            $visibility = "Internal Only";
            //Hitory Helper function
            history($user_id, $object_id, $model, $status, $comment, $visibility, $user_model);

History function

function history($user_id, $object_id, $model, $status, $comment, $visibility = '', $user_model = null)
{
  $history = new History;
  $history->user_id = $user_id;
  $history->object_id = $object_id;
  $history->model = $model;
  $history->status = $status;
  $history->comment = $comment;
  $history->visibility = $visibility;
  if (!empty($user_model))
    $history->user_model = $user_model;

  $history->save();
  return "history inserted";
}

But I personally think that there could be a better approach, I heared that this type of things should be done by events but why and how I dont know.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3mr5wui
via IFTTT

Aucun commentaire:

Enregistrer un commentaire