staticcode's avatar

Relations in model or controller

Hi guys,

I am having a discussion here about whether to make relations directly in the model, or just write joins and stuff in the eloquent query (controller) it self.

Personally I prefer writing the joins and relations myself (old habbit) and personally I do not see the (large) benefit of doing relations in the model.

Could you guys please give me some intel on your own workflow, and the pro's and con's for doing it in the controller or model way?

Best regards

0 likes
3 replies
bobbybouwmann's avatar

The advantages of using the relationships is that Laravel is responsible for creating your queries. They are secured by default and it cleans up your code. I prefer to use relationships because they provide a natural code language. For example: the user has many posts. You know exactly what the relation is and how it's setup.

Creating your own queries, joins and so on might give you more control, but it also gives you more code which means more technical debt!

That's my 2 cents ;)

1 like
staticcode's avatar

Very good "2 cents" lol, helpful information thanks for your reply Bobby!

Please or to participate in this conversation.