0

I want to correctly handle user store form.

I consider which patter will be the most correct/popular. It is repository pattern? Service pattern?

Another difficulty: User form: name, email, postal code, city, street. I want to create two models in one form: User and Address. How can I solve it? I throught about:

// UserController store(UserRequest $request)
$address = $addressRepo->create($request->validated());
$user = $userRepo->create($request->validated(), $address)

Issues i'm wondering about:

  • UserRequest has data connected with user and address

I am confused as to what would be the most correct.

xbiu
  • 33
  • 1
  • 9

1 Answers1

1

If your app won't be more than a CRUD, don't bother with repository pattern etc. Just use MVC pattern.

If you really need to use repository pattern you can use it also, dependency injection.

You can also create a UserService, which you send $request->validated() data to your service.

You should inject your repositories to this service. Your service should hold your logic, Repository should do store update etc.

Your contoller:

UserService::createUser($request->validated());

Your service:

...
$userRepository->createUser(...);
$addressRepository->createAddres(...);
gguney
  • 2,512
  • 1
  • 12
  • 26
  • The application is to handle complex crud operations (a lot of business logic). This is my first such large application. Is the repository-service pattern widely used these days? – xbiu Mar 12 '22 at 09:19
  • Yeah ofcourse, especially you have so much business logic and duplicate code – gguney Mar 12 '22 at 10:28