jeudi 20 décembre 2018

What kind of design pattern or code organization I should apply?

Let's assume I have User model that related to models Image, Card, etc. That means when I create a new user, I should create Image and Card instances along with it, e.g. I have something like this in my controller's store method:

DB::beginTransaction();
try {
  $user->saveOrFail();

  $images = $request->input('images');

  foreach ($images as $image) {
    $imageObj = new Image();
    $imageObj->user_id = $user->id;
    $imageObj->url = $image->url;
    $imageObj->saveOrFail();
  }
  ....

  return redirect('some');
}

Is there any design pattern or something that can handle this kind of situation in the right way? I read Repository and Factory design patterns, but I think they are helpless in this case.

Aucun commentaire:

Enregistrer un commentaire