Skip to content
Advertisement

Laravel 5 – get ID by other field properly

I want to get the ID of a user in a middleware by searching with his email. I thought about doing it like this in one of two ways.

First, creating a function in the model User:

JavaScript

Second, using this function in my middleware

JavaScript

And it doesn’t work, nor am I even sure it’s the right way…

Advertisement

Answer

This should do it. No need for extra methods in model. Eloquent makes everything pretty simple.

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement