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

gpalmer711's avatar

Advice for correct way to add specific code to laravel project

Hi All,

I'm fairly new to Laravel and want to make sure that I am developing my app in a way that other developers will be able to pick up easily. I'm not sure of the best place to put some code to follow best practices and wondered if I could ask your advice.

PHP: 8.2 Laravel: 10.X

The app is eLearning platform, The important info is that there are models for (friendly names below, not actual names):

  • Users
  • Learning Plans
  • Playlist
  • User To Learning Plan
  • User To Playlist

Learning Plans are a collection of X number of playlists. A playlist contains all of the content for a specific topic. A user will be assigned to a playlist, the user will then get access to the playlist content at specified dates. based on the Learning Plan settings. The User Playlist contains the link between the user and playlist, along with date available from, date completed, and some other info.

I have created a UI that allows an admin to assign a playlist to 1 or more users. It is the code that will add the records to the User To Playlist model I am not sure where to put.

Functionally I can see that it would work in the relevant controller, as a separate class, or as a listener waiting for an event that is triggered when a new record is added to the User To Learning Plan model.

Is there a Laravel way of doing this?

Thanks for reading and for your responses.

0 likes
3 replies
automica's avatar

your controller layer should be pretty light, and use it as a means to

  • handle traffic from a route
  • do a thing
  • return a response to describe what you did.

the 'do a thing' should be done in a separate class, and commonly this would be placed in App/Services/* which an accompanying test within tests/Unit/Services/*

if you use events and listeners, it is still helpful Ito isolate your new Service in its own namespace as that will allow you to share this functionality across your codebase. Eg you might have an event that does a thing but also that thing can be ran by running a custom artisan command.

The key thing to take home is ensure new work is covered by tests as that will mean you can easily move functionality as you refine your directory structure.

1 like
martinbean's avatar

@gpalmer711 It’s probably worth taking a step back and planning your models and their relations on paper before diving into code, because if you go steaming ahead and model your application “wrong”, then it becomes a tough job undoing all of the incorrect models you’ve created, and incorrect relations between them.

That being said, when you do come to code, don’t fight the framework. If you want something to be able to pick up your application and start working on it straight away, then follow the framework’s conventions. Put models in the app/Models directory; put controllers in the app/Http/Controllers directory; and so on.

I’ve been working on the web for over 15 years now (and 10 of those with Laravel) and the projects that were “difficult” to work worth and difficult to onboard new developers on, were the projects where someone had decided it was a special little snowflake and they needed to do things in their own unique way, which means someone has to then sit and explain all of these decisions and deviations instead of just going, “Here’s the source repository for the app. Clone it to your computer and get going”.

1 like
gpalmer711's avatar

Thanks for the advice guys - much appreciated.

Please or to participate in this conversation.