Skip to content
Advertisement

How to correctly handle laravel form?

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.

Advertisement

Answer

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(...);
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement