Laravel 5.4 #14 : Query scopes and presenters

Today we will use the scopes to show the active users in our web. Although anyone can currently register, later we will add a package of Laravel that allows managing the roles and limit, depending on the role, who can post posts. And we will also format the publishing date of the posts.


Scopes are functions that we add to the model that allow us to reuse parts of queries so we do not have to repeat them many times. Open the model  User.php and add this code:

And with this, we will be able to easily return active users:

Display them in the view:


As you can see, use scopes is very simple and with them we can save writing code, if it is a query that we use several times.



Presenters are functions that manipulate the data in the database before displaying it in views. We apply the logic we want (format the date, join two fields, etc.) and then display it in the view.

To create a presenter, I have based on this post.

First, we have to create a Presenters directory in app. And later create a DatePresenter.php file. What our presenter will do is format the creation date:

Add these lines to the Post.php  model:

Add to the view this line:


Done this, we would have the date formatted. We can apply it to any other field and put the logic we want.


– Presenters: