Support the ongoing development of Laravel.io →
Database Eloquent Architecture
Last updated 2 years ago.
0

I put model's business (as in "none of your business") in model's file. Let me explain with an example.

class Post extends Eloquent {
    public function approved($isApproved)
    {
        $this->approved = $isApproved;

        return $this;
    }
}

class PostController extends BaseController {
    public function setUnapproved($id)
    {
        Post::find($id)->approved(false)->save();

        // redirect etc.
    }
}

The reason I do it this way is for two reasons. First is, you may approve or unapprove posts from more than one place, right? No need to copy paste the same particular process everywhere. Second is, a controller doesn't have to know how to unapprove a post. Putting it in the model is also probably not the best way, we should use a repository or similar, but it is better than the controller way.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

ottz0 ottz0 Joined 15 Nov 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.