Eloquent relationships with where clause

I think this blog will one day be renamed “things of Laravel I would have known before if I questionned the Framework”.

Jean-Philippe Murray
jpmurray.net

--

So I was there, trying to build a really small and simple event system for a project I’m working on. Basically, events can happen to models on this project, and I want to keep track of them, their activeness and completeness.

Playing with different solutions, I though “wouldn’t it be nice if my event class (and its relationships) would be able to act for every models I would have liked?” Turns out, this is really easy to do because in the end, Laravel is just PHP and it’s not because the framework documentations says “this is how you create a One-To-Many relationship” that other methods of PHP, or even the framework doesn’t apply there!

Including this trait to my models enables them to have events.

Now, with this trait, I can fetch all of the model’s events (I should rename the method to “getEvents”), but of course I can’t create some using `$model->events()->create()`. There is certainly a better way to accomplish the creation of events, but my workflow for those events will typically be looking like this, and it works for me needs:

I’m often quick to say read the fuckin’ manual, but this mindset keeps me away from the fact that to do something, you might just need to try it.

--

--

Studied religions and its people for some time, now I’m back working with the web. Passionate about technologies, the human being and the human becoming. FR/EN