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