cameronasmith's avatar

Can you place some logic in a Model?

I know based on the MVC principles you are suppose to put your logic within the controller, but I was wondering if sometimes it was appropriate to put some logic within the model itself. For example, this is in my controller:

private function fetchContactId($collection) { if ($collection->to == Auth::User()->id) { return $collection->from; }

    return $collection->to;
}

It basically finds out if the row from the database belongs to the send user or the receiver user. My question is was I right to put it in the controller and not the model and when might you put things inside a model?

Thanks,

Cameron

0 likes
9 replies
InaniELHoussain's avatar
Level 32

if I were in your position I'd do it in the Model since its related to the Model it self ...

1 like
cameronasmith's avatar

Laracasts seem to be having issues at the moment so I cannot mark your answer as correct. But, as soon as its working again I will mark it correct.

Thank you!

1 like
skliche's avatar

@cameronasmith Logic in a model is perfectly fine! The model should actually contain the business logic. Logic that is not tightly coupled to the exact application you are writing but that could be used in a different application as well.
The controller contains application logic that is specific to the application and is used to interact with the business logic. This is a simplified version of my own understanding and might not follow any design principles books. But I think it makes sense.

In your case I'd say it depends on what exactly you are going to do with that method.

It's a step further but you might find this lesson helpful: https://laracasts.com/lessons/where-do-i-put-this

cameronasmith's avatar

Hi Guys,

Does anybody know how to get a property inside the mode the model?

For example:

$user = User::find(1)

$user->test();

// inside the Model User

public function test()
{
    if ($this->id == 1) {
        return true;
    }

    return false;
}

thanks,

Cameron

skliche's avatar

@cameronasmith That's exactly how it works ...

// app/User.php
class User extends Model {
    public function test() {
        return $this->id == 1;
        // or
        // return $this->getAttribute('id') == 1;
    }
}

// app/Http/Controllers/SomeController.php
class SomeController extends Controller {
    public function index() {
        $user = User::find(1);
        $user->test();
    }
}

Are you getting any errors?

1 like
jekinney's avatar

For a lot of projects using a model with methods like a repository pattern is fine. As @skliche mentioned I use a lot on small to medium sites that have just a few admins, generally small business or personal sites. I personally don't prefer to use actual queries (eloquent, query builder or raw) in the controllers mainly for reuse ability and more importantly one place to update vs finding the same query in different spots.

Bottom line I urge consistency though. Nothing imo is more frustrating then updating code that has queries in the controller and some where else. Makes a five minute update take hours that's not needed.

1 like
cameronasmith's avatar

Hi Jekinney,

I just retested my code I forgot to foreach through each of my statements that's why it wasn't working.

So, I'm going to start putting all my queries within my models, as you suggested. However, will this not make the models larger overtime?

Also, "model with methods like a repository pattern is fine" please could you elaborate more on this. I don't really know what a repository is can you recommend any video tutorials on laracasts?

Thanks,

Cameron.

Please or to participate in this conversation.