A
A
Alexander Pankov2022-04-20 18:24:07
PHP
Alexander Pankov, 2022-04-20 18:24:07

How to organize code in laravel?

Prompt how to organize "architecture".
1) I have an order (order->item->product), those order model is associated with the item model (this is the order basket (product in the basket)), and the basket element is associated with the product model (catalogue product)
2) the item model stores the price and quantity of the purchased goods from the catalog (product) in the order (order)
3) there is a form where the client can change the parameters of the goods in the order (item), for example, add quantity or change the price
4) when sending this form, I simply (in one line ) I change the data in the database $item->update($request->only(['price', 'quantity'.....]) all this is one controller line

5) NOW THE QUESTION, after changing, for example, the quantity, I need to recalculate some order parameters, for example, the parameter "total amount" or "delivery amount" or "order status" and so on, not the point, the point is that in the controller that answers for changing the product in the cart, I suddenly start working with the order and its parameters - this is not according to GRAPS, SOLID (sole responsibility, information expert), which path (pattern) to choose, in the context of Laravel?

there are observers, there are events and listeners, there is a "service layer" (whatever that means)... my
head is spinning

, tell me how you would do it, where and how should I recalculate the order itself, after changing his list of goods?

in my head
1) create a service class
2) initialize it in the controller constructor
3) in the controller method I change the basket data in one line
4) immediately below I pull the "update order" service method,

but on the other hand, nothing prevents another programmer from updating the basket data
and that's it, not recalculating the order ... how to protect yourself from this?

what is the best (correct) way to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vism, 2022-04-20
@PankovAlxndr

tell me how you would do it, where and how can I recalculate the order itself, after changing its list of goods?
in my head
1) I create a service class
2) I initialize it in the controller constructor
3) in the controller method I change the basket data in one line
4) I immediately pull the "update order" service method below,
but on the other hand, nothing prevents another programmer from taking an update cart data
and everything, do not recalculate the order... how to protect yourself from this?

Here's the word, bright head! think of another programmer.
And everything is simple, no need to update in the controller.
Make a service class, where there will be update and recalculation methods.
And do it right there.
Those. a method to update the cart data, which will update and call the method of the same class to recalculate the order.
And do not forget to wrap all this in one transaction.
And then the lyrics...
but on the other hand, nothing prevents another programmer from updating the cart data

But it still won't stop him from zagovnokodit.
In order to prevent shit coding, you need at least symphony to make complex true code structures that take 10 times longer to write than a service class in Laravel. And 10 times more you can be smarter yourself (if you asked a question here, to which I answer, then it’s too early for you).
And to make everything work-true, you need to fasten the tests, which will still multiply all this by x3 in time and also will not give guarantees :)
And most importantly, the business will ask you how you are doing right now with the risk that someone will make a bug and then fix it than doing everything according to the canons 10-30 times longer, more expensive and increasing the input for programmers very much. Well, there will also be bugs, only much more complex and difficult to catch.
Over my 12 years, I realized that I need to do what the business wants, and this is the simplest, most understandable code and sometimes fix bugs. And ALWAYS clean up technical debt.
626035402f57d385357678.jpeg

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question