-
Notifications
You must be signed in to change notification settings - Fork 28
Pitfall: Auth::user()->with('company')->first() #2642
Comments
How bout |
Yes, I know. This works:
Still |
Running Chaining As above, if you already have a model instance, the appropriate course of action is to call The lazy loading being prevented by that functionality would be calling |
I am not looking for a method to load my relationship. I am complaining that the code does not what it looks looks like from a clean code perspective.
|
Today I tried
Model::preventLazyLoading(! app()->isProduction());
and walked right into this pitfall.I changed
to
While the first line returns the logged in user.
The second line returns the first user in the database.
After I figured out my mistake I felt stupid about it, but I talked to some other developers and they all had the same WTF moment.
Well, if you like me did not realize the problem:
with
is a static function that is called on the already loaded user object. LikeUser::with('company')
this creates a new query that has nothing to do with the user returned fromAuth::user()
.This actually a PHP "problem" as you always can call statics functions from the object.
I was wondering how this pitfall could be prevented and found a magic solution.
As this is the real world I am not very happy with magic hacks to fix a problem.
What do you think about this?
I am the only idiot and this does not need to be fixed or do you agree?
Maybe even have a good solution?
The text was updated successfully, but these errors were encountered: