Hello. I am writing a CRM system on Laravel, for the first time I am faced with the validation of a large amount of data. Faced a problem when adding a new order to CRM.

What you need to check before adding a new order:

  • Phone number. It is clear that the validation of the numbers is added to the StoreOrderRequest .
  • Is there such a client in the database (search by number). It is clear that Customer::findOrNew() .
  • Is the item currently available for order?
  • Is the delivery address specified (if yes, then you still need to add a task to clarify the delivery address)

And so on. The list is quite large; it will expand as the functionality grows. But so far I don’t understand, precisely from the ideological side of Laravel, where exactly should I shove all the data validation? It is clear that it is the input data (client name, client phone number) that we check in StoreOrderRequest , but where is it better to take out all the business logic, how is this implemented?

There is an OrderController , there is a public function create(StoreOrderRequest $request){} , but it’s not good to make the controller thick.

How correct is it to make the Event new order, and parameters such as checking for the delivery address to be placed in a separate Listener and implement the business logic associated specifically with this parameter in this particular "listener"?

I want to know the opinion of more experienced developers, it is architecture that is of interest in this case.

  • Идеологически , Laravel wants you to validate in the Контроллере . BUT, the reality is that such an approach affects scalability for the worse, so validation is usually carried out in моделях . - Manitikyl

2 answers 2

You will have a broad, multifaceted validation, so it will need to be performed in the class of the StoreOrderRequest query when parsing it from the form data. For example, create a request object and pass it the form data in the constructor, and then calling its validate function.

After that, get the filled values ​​of its model properties that you need. This can all be wrapped up with its own function (parse, for example), which returns a list of errors, or, in their absence, fills model properties from these forms. After that, it will be possible to check these properties-models.

As for messages, I can say that they were created for the operation of independent parts / modules of the project, and not for the immediate processing required in the logic of the controller or the class of the request. If the work performed is asynchronous, you will win using them (but this again only if the messages are also asynchronous, that is, they work in a separate process, outside the process of the request context).

    You think correctly on how to keep the controller thin. Following the good practice of designing Laravel applications, you should make thick models and thin controllers.

    At the same time, the actual business logic of the application, i.e. following order processing processes, etc. can be taken out in a separate service that can be accessed from any controller.

    Various notifications, etc. you can put it into some universal NotificationsListener and generate events for it to send messages.