0

I am using eloquent as part of a slim 3 project with slim-jwt-auth (https://github.com/tuupola/slim-jwt-auth), outside of Laravel. There are many situations where I want to control the items returned based on the user who is making the request.

From what I understand though, there is no way to add extra parameters such as user_id for use in eloquent global scope.

I found one blog post that initially looked helpful, as it does exactly what I am trying to achieve (http://www.sdavara.com/Eloquent-Global-Scopes-A-cool-and-easy-way-to-fetch-loggedin-user-data/), but it relies on Laravel Auth.

The fact that my solution needs to work globally is important, as I have many nested relationships that should also be filtered by user id when queried.

Could anyone suggest what might be the best approach for achieving this?

Peter Hall
  • 53,120
  • 14
  • 139
  • 204
Finglish
  • 9,692
  • 14
  • 70
  • 114
  • You basically need a way to detect which user is currently sending the request, then you want to add global scope to eloquent models to filter queries based on some user properties, like user_id, right? – Nima Aug 20 '17 at 20:23
  • basically. I have a user object as part of my request, decoded from jwt in middleware, but eloquent doesn't allow me to pass parameters to global scope definitions, so I am not sure how I can define it. – Finglish Aug 21 '17 at 03:37

0 Answers0