I have a very strange Laravel issue that I can't explain and that has happened to me before without me being able to solve it.
To explain, some (not all) of the relationships do not load when I load them via eager loading.
For example, I have my User model and a Tag model. My user has 3 tags in the exemple. This is a classic belongsToMany relationship.
public function tags(): BelongsToMany {
return $this->belongsToMany(Tag::class, 'user_tags');
}
I want to retrieve tags of one user :
$user = User::with(['tags'])->findOrFail($randomId);
So, my user has 3 tags but when i'm doing the dd of the model the tags relashoship is empty (strange). But if i'm using the withCount method, tags_count equals 3. Moreover, if I do :
$tags = user->tags()->get();
I can retrieve the three tags.
So eager loading does not work only in some relationships, but i have an other strange situation. I wanted to know if it was my local environment. I deployed my software on a server and some of the broken eagerLod relationships are working in prod and some others are not still working...
I'm a senior laravel developer and after 3 days of search, I need help to find other resolution paths.
Thanks
If it happen to you, deploy on a new database.