Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dertechniker's avatar

Laravel /PHP Structure / General questions

So, I don't even know how what to properly name this thread.

Anyways, I know there might not be THE right answers for this, so I'm just going to ask for opinions on a few things here.

Background: I had some bigger PHP Apps like 7-10 years ago, but then stopped developing. My Apps back then were written procedural and a mess from start to finish. I never really used a framework and did little OOP coding and nowadays i'm pretty sure i did not even know WHY the stuff i wrote even worked.

Anyways, a few probably not so bright questions:

1.) Example: I have a - lets say - url shortener app. A user of course can have many URLs. I have a database with a "users" table and a "links" table which stores the links along with a user_id. Now, having learned i can tell the laravel "User" Model that there is a relationship to the "Links" table ( hasMany('Link')). So now if i want to show the User all his links, would you do a mysql query on in the "Link" Model with a WHERE with the user_id, or would you use the User->link() function from the "User"-Model in the "Link" Model? Is there even a better way or is one just significant worse than the other or doesn't it even matter?

2.) Where to put what? I always thought of a rule of thumb: Everything that interact with the database is going into a Model, everything else is going into a Controller. So as an example: I want to get a Users registration details. Then the UserController.php would call a function at the User.php (Model), the User.php simply querys the database and returns what the database sends back. All other things (like "if-clauses" if there are any, filtering) is done in the controller. Is sthat still a way to go or not? As i heard about keeping the controller slim. But if i want to keep the controller slim, where do i put all the stuff where i have to process some data?

There sure are other things i just can't wrap my head around which i'll post in this thread instead of opening a new one. As said: It's probably more about different opinions than receiving a definitive answer.

0 likes
4 replies
bashy's avatar

Have you watched any of the videos on relationships etc?

dertechniker's avatar

Thanks for the answers, will watch those videos - especially the where-do-i-put-this I sometimes am overwhelmed with the amount of videos and topics on Laracasts. While the collections feature on here is nice, it's still somehow hard to manage your way through all the informations.

The urlshortener was just an example not that i actually build an url shortener.

chrisgeary92's avatar

Oh yes I assumed that, but the URL Shortener Jeffrey creates has more than just Controller/Model classes. So it would be a good one to watch, and compare what you've mentioned above with what Jeffrey creates.

Please or to participate in this conversation.