N
N
naneri2014-12-15 09:44:36
Laravel
naneri, 2014-12-15 09:44:36

How to correctly move a function to a model in Laravel?

I have the following function call in the controller to get the latest topics.

Topic::join('blogs', 'topics.blog_id', '=', 'blogs.id')
       ->join('blog_subscriptions as us', function ($j) use ($userId) {
          $j->on('us.blog_id', '=', 'blogs.id')
            ->where('us.user_id', '=', $userId);
        })->get(['topics.*']);

How can I correctly transfer it to the Model, so that then I can simply call it with a function
Topic::getSubscribedTopics($userId);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gladkovskiy, 2014-12-15
@SMGladkovskiy

Implement via the "Repository" pattern and store similar constructs in the repository without cluttering the model and controller. This is business logic and it would be nice to separate it from the application.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question