Laravel 5.4 #7 : Middlewares and relationships with Eloquent ORM

Sometimes, you don’t want that certain users can access to certain parts of the web, either because of their role, because they aren’t the creators or for whatever reason. Laravel has a feature that allows us to do this very easily, the so-called Middlewares. This is what we are going to try today, together with database field relationships with Eloquent ORM. So, we will learn to assign a post to its creator and then how to access that data.

Assign a post to its creator

First of all we have to associate a user to the post, that we are going to do it through a field in the table posts called user_id. To create it, as always, we have to create a migration:

Open it:

Execute it:

Now we have to modify the models post and user, to indicate that there is a relationship between these two tables across the field user_id.

Open the user model and add this method:

Open the post model and replace with this:

We have already indicated that there is a relationship in the database between these two tables.


Now we will hide the post submission form if you aren’t logged in, because now it will not make sense to show it, since when it tries to save the post, it will not be able to access the user id, because there will not be any user logged in. Open the file where the form is and put the two includes like this. With this, it will be hidden if you are not logged in:

Now simply modify the method of creating posts and add the id of the user, which will be the id of the user who is logged in.

And it would be saving the user id and hiding the post submission form.

If we want to show the user object, we will have to put the next line where we want:


You can find more info about the different types of relationships and more things, here.



A middleware is a function that is executed before access to the URL that we address, in other words, it acts as a filter and lets pass to those who meet the requirements that we add.

For example, it makes no sense that someone not logged in can access to URL POST (method store) of posts creation, therefore, we will restrict access so that only logged-in users can access. Open the controller and add this constructor:

We are saying that apply the middleware auth (it comes with Laravel) just to the method store.

We have already restricted the access.


But there are times that it is not enough to simply check if it is logged or not, maybe we want to check its role, what country it is, etc., etc. Then we must create a custom middleware. For this, as always, Laravel provides us with a command that creates the middleware in the appropriate folder very easily

Note: this middleware we will use later when we do the editing and deleting posts. Although there are other better ways (for example, policies) that we will explain and apply, but at the moment this is useful for us.

We will have created a file in the middleware folder. Open it and put:

We are checking the post creator and if is this user, we let him continue, if it isn’t, we redirect him to the list. We still need to register it, for that, we open the Kernel.php file and add this line to the array of middlewares:

To use it, we have to use the previous syntax to add middlewares, also you can see here that there are other ways to do it.