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

Fijvect's avatar

Model manipulation before every call

Hello! I am hoping for advice on how to best approach my problem. Strategies or tips.

I have a website with repeating events & classes. They all repeat using iCal standard, and I decided to calculate each event, rather than store each instance independently in the database. So everytime I get an event, I have to run functions (that I currently have swept under the rug in a Trait) that calculate the instance of the event with a given date in the route uri.

So my question, is whether it's possible to run these calculations BEFORE the model hits the controller. Unfortunately these calculations need a composer package to run... In case that's important.

My urls related to events all look like:

Route::get('/{eventsOrClasses}/{event}-{slug}/{date}', EventController@show);

(fyi: since events and classes run exactly the same, I use the eventsOrClasses variable to switch from an Event model to a Class model which both extend the same base class. I do this switch with route model binding checks)

I appreciate any insight you can give me!

0 likes
3 replies
Fijvect's avatar

Might also be worth noting that I use the calculations MANY controllers, since there are a lot of related functions to classes/events: ticket sales/returns/admin-management/etc., attendee management, and more. So this will be used through a huge chunk of the website.

Snapey's avatar
Snapey
Best Answer
Level 122

Can't it all be in a service provider (or repository class), and then inject that into your controller instead of the model?

Fijvect's avatar

Thanks for the reply! So correct me if I'm wrong, but for the repository, wouldn't I have to inject it in the constructors, and run the calculation functions still from within the various controller methods?

As for the service container strategy. I haven't played much with those yet, but when researching it didn't seem like the place for large complex business logic. But I don't have much experience with those so I'll keep researching! I'm still semi-new with Laravel, and there seems to be no end to tricks to learn, lol.

Please or to participate in this conversation.