Once you start actually building something with Laravel, it’s easy to find yourself repeatedly typing the same code when you fetch relationships. This article shows you a centralised approach using scopes.

Image for post
Image for post
Photo by Brett Jordan on Unsplash

Setup

Imagine you have models for Blog and Post and a relationship between the two — Blog hasMany Post.

A Post has a datetime field called published_at — but your posts aren’t necessarily in order of date published, as some of them take longer to proof than others.

Problem

Across your site, you need to show the 3 most recently-published posts for each blog. Eloquent makes this easy:

But this…


There is much vitriol and misinformation around the 2017 United Kingdom election. Social media is a self-referential echo chamber and the mainstream media have shown a lack of balance — so here’s my take on it.

Image for post
Image for post
Image courtesy Elliott Stallion

So being realistic, only Labour and the Tories have any chance of getting in — so I’ll limit my comments to those. You’ll soon see that I’m intending to vote Labour — regardless, I’ve tried to read a balanced set of articles and question everything.

Brexit aside, this election is about investment in people

Both the Tories and Labour are committing themselves to going ahead with Brexit negotiations without an option of a second referendum — so we can skip over that and focus on the fact that many of our social systems are struggling. …

Adam Hopkinson

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store