A
A
Alexey Sklyarov2021-03-06 18:49:36
Laravel
Alexey Sklyarov, 2021-03-06 18:49:36

Where is the best place to add additional data to a query?

There is a need to indicate the status of the post (in moderation or published) depending on the role of the user who posts or updates this material. But there is a problem with understanding exactly where to add data to the request. I have several options:

1. Create a middleware and add post_status to the current request, which is calculated based on the user's role.
2. Use the method in your custom Request: prepareForValidation().
3. Change in the observer (the worst option).

Which of these options should be used, what additional options are there, because it will not always be necessary to attach some computed property to the request?

Pitfalls that are confusing: in the case of middleware, for each element added to the request, I will have to write my own middleware (for example, attaching a post status, attaching a commentator's ip to a comment, attaching a user_id, etc.).

In the case of my Request, I can attach the data that I need EXACTLY for this request, for example StorePostRequest, UpdatePostRequest and so on, but I'm not sure that from a logical point of view it's okay to add some data to the request in prevalidation (the documentation describes changing some already existing fields in the request).

In the case of observables, everything is generally bad, because there is only one model, and for example, if the admin wants to update the post, but does not change its status (published or under moderation), the script will still assign its status depending on the user role, and for the post admin it will always will be published, and not remain on moderation.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin B., 2021-03-06
@Kostik_1993

Neither an observer nor a request is needed here at all.
In your case, all data is managed by business logic in the place where it is executed. If we omit all development principles, then the controller method will be the conditional place of execution. Well, or the business class that it will call, which is more correct. I imagine working with the code after you, I go in, prescribe the status that is required in the new task, but it is still set to some other one and I start looking, but where did Aleksey Sklyarov put his change

J
JhaoDa, 2021-03-06
@JhaoDa

Let's imagine a situation where the project will need the ability to create a post with a console command (not very realistic, but still) - all of a sudden, option # 3 remains the only one.
True, it is advisable to use not observers (they, in general, are not very good for logic, the more it is, the more they begin to resemble a pasta monster), but some kind of service classes that will contain all the necessary logic to create a post, since it is more complicated than the simplest CRUD.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question