What action would be taken; this looks like it returns an Eloquent Builder only?
Moving from service to... Action (Eloquent query)
Hi,
What about something like that ?
I had a method inside my service class, the method returns this :
Tag::popular()->take($total)
What about Eloquent query inside Action ? How can i do that ? Juste use Tag::popular()->take(15) inside my controller ? It sound like a bit overkill to have an Action class for this...
Thank you for your help !
@tykus Thank you for your answer.
It was like this :
public function popular(int $total): Builder
{
return Tag::popular()->take($total);
}
Do you mean it should be like this?
public function popular(int $total): Collection
{
return Tag::popular()->take($total)->get();
}
I was interested in setting up Actions. For example, I have an action: CreateTag, etc.
But for very simple queries like this... I find it a bit overkill, or else I don't understand something. I find it made sense in a service with the other methods... and I also find a CreateTag action makes sense. But for a simple request like this... I'm a bit lost.
Thank you again ! (and sorry for my english, i'm french)
@bloup so why are you changing?
@bloup what was it, a Service class? On the face of it, it looks like you;re over-engineering a local Scope. What is the context here?
@Snapey Very good question. Note that this project is personal, so I test things.
Regarding the Actions, I find it better especially for CreateTag... I find it cleaner.
But by removing these methods from the service, I can't imagine leaving the service just for the popular method.
Unless you leave the service and call the actions there... but that would be a bit of a duplication, it seems to me.
Thank you for your reply.
@tykus Yes, it was inside TagService :)
So you mean it's good practice to directly call like this inside controller ? (I just display the 15 most popular tags in the ui)
Tag::popular()->take(15)->get();
Thank you for your reply.
especially for CreateTag
That is an action; there is a side-effect, i.e. a new Tag. As I mentioned above, the popular method seems it should be no more than a Local Scope on the model class
@tykus Thank you. So my first feeling that it was overkill seems to be good.
It is acceptable to have this directly in the controller then ? Tag::popular()->take(15)->get();
I was probably a little too "school" trying to isolate everything.
@bloup how do you use 'popular tags' ? Is it just in this controller or in other controllers also?
@Snapey I use it in three different places. Thank you :)
@bloup if its not related to the controller and is just for inclusion on the page layout, consider a view composer instead.
@Snapey Perfect, I did not know this solution and it fits perfectly in my case :)
Now imagine the case where an Eloquent query is used in multiple places, not specifically related to views.
Is it good practice to repeat an instruction like:
User::scope()->get();
Or would it be better to isolate it?
I'll stop my questions after this one.
Thanks again.
@bloup its fine in my book
@Snapey Thank you. I feel like KISS.
Probably a bit of a matter of taste. Some will probably say to set up a Repository but I'm not convinced in Laravel.
Thanks again for your quick responses.
Please or to participate in this conversation.