Open allandantasdev opened 3 months ago
Hey there, thanks for reporting this issue.
We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as one separate commit on the main/master branch and share the repository here?
Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.
laravel new bug-report --github="--public"
Do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue.
Thanks!
Additional info:
This is the query generated by Auth::user()->categories->pluck('id') at Example@scopeHasAccess:24 while lazy loading
And is the same query generated after trying to eagerloading Category::with('examples'):
Thank you for reporting this issue!
As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.
If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.
Thank you!
I found a quick and dirty solution, in the constructor of Relation
(beware that it breaks some other cases)
public function __construct(Builder $query, Model $parent)
{
$this->query = $query;
$this->parent = $parent;
$this->related = $query->getModel();
$this->addConstraints();
static::$constraints = true; // This
}
I've been working on getting a proper fix, but there doesn't seem to be a straight path forward as changing one thing breaks another one, this will likely require some debug_backtrace to fix this without breaking some test cases
Laravel Version
11.7.0
PHP Version
8.3.7
Database Driver & Version
PostgreSQL 15.7 and MySQL 8.0.37
Description
When eager loading a model relationships, the results differ from when they are lazy loaded. This issue occurs whenever a Relation is called inside the definition of another Relation, but only when eager loading is used on the main one.
After investigation I realized that the cause lies in Illuminate\Database\Eloquent\Builder@eagerLoadRelation:
Which calls the Illuminate\Database\Eloquent\Builder@getRelation method:
Which gets to the root cause of the problem in Illuminate\Database\Eloquent\Relations\Relation@noConstraints:
The method Illuminate\Database\Eloquent\Relations\Relation@noConstraints is called during eager loading and uses a boolean attribute to manage the constraints. However, this flag is static and seems to be causing the where clauses of other relations to be omitted, leading to incorrect results.
Steps To Reproduce
Create the following seeder:
class Category extends Model { // ... public function examples(): HasMany { return $this->hasMany(Example::class); ->hasAccess(Auth::user()); } }
// Authenticated user: 1 // - Category 1: 1 examples" // - Category 2: 1 examples" // - Category 3: 3 examples" // ---------------------------- // Authenticated user: 2" // - Category 1: 1 examples" // - Category 2: 0 examples" // - Category 3: 2 examples"
Expected behavior: The fetched relations should be consistent regardless of whether they are lazy or eager loaded.
Actual behavior: