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

rosswintle's avatar

Doing something on Model creation

Note: I'm good with PHP and pretty OK with traditional inheritance-based OOP, but an struggling with what seems to be some pretty basic stuff in Laravel.

I have a Week model, and a Day model. Whenever I get a an instance of a Week I want to subsequently be able to do something like :

$week = new Week( <some attribtues> );
foreach (Week->days as $day) {
  <some code>
}

How can I create the days (even if they are not currently created) whenever I get a week? And how do I make sure that the days are returned as part of the Week model?

I can kinda see how I can do this using logic in a controller, but it doesn't seem to be the right place.

I can kinda see how I can create the days using events, but I don't then see how to get the days back into the Model.

This seems like a pretty basic thing to want to do: created some related objects whenever you create an object. But I'm really struggling.

Any advice?

0 likes
4 replies
rosswintle's avatar

Thanks.

Yeah, that looks like a potential way. But I'm concerned about hiding functionality related to my Weeks in a place that doesn't have much to do with Weeks. I don't want to come back to this code later and ask "Where does week create the days? It's not in the model or controller. Argh!"

Are there any tips for structuring event (like, can I create them in my model file?) or annotating them so that the functionality isn't hidden away from the code for the Weeks model?

Please or to participate in this conversation.