JeroenvanRensen
3 months ago
666
10
General

Do you move controller methods to models or not?

Posted 3 months ago by JeroenvanRensen

Hi everyone,

I'm watching a lot of Laracasts series, and something I noticed was that Jeffrey moves a lot of his controller actions to models.

For example ReplyController:

public function store(Thread $thread) 
{
    request()->validate([
        'body' => ['required']
    ]);

    return $thread->addReply([
        'body' => request('body'),
        'user_id' => auth()->id()
    ]);
}

And Thread.php:

public function addReply($reply)
{
    $reply = $this->replies()->create($reply);

    event(new ThreadReceivedNewReply($reply));

    return $reply;
}

However, I personally prefer to do it this way:

ReplyController.php

public function store(Thread $thread)
    request()->validate([
        'body' => ['required']
    ]);

    $reply = $thread->replies()->create([
        'user_id' => auth()->id(),
        'thread_id' => $thread->id,
        'body' => request('body')
    ]);
}

What do you think, and what are your preferences?

I like to hear your thoughts.

Thank you! Jeroen

Please sign in or create an account to participate in this conversation.