Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eager loading latest item of relation does not return expected results #2437

Open
stefanschindler opened this issue Aug 23, 2022 · 5 comments

Comments

@stefanschindler
Copy link

stefanschindler commented Aug 23, 2022

  • Laravel-mongodb Version: 3.8.5
  • PHP Version: 8.0.15
  • Database Driver & Version: unix 3.4.24

Description:

I have a hybrid setup with MySQL and MongoDB.
Eager Loading latest item of relation is not working correctly. I want to load a list of customers with their latest metrics (by date) eager loaded. Instead of loading the latest metric for every customer, it just loads the latest metric in total.

Steps to reproduce

The Customer Class (Mysql):

class Customer extends Model
{
    use HybridRelations;

    public function metrics()
    {
        return $this->hasMany(Metrics::class);
    }
}

The Metrics Class (Mongodb):

class Metrics extends Moloquent
{
    protected $connection = 'mongodb';
   ...
}

The code that loads the data:

$customers = Customer::orderBy('last_accessed_at', 'desc')
                                                  ->with([
                                                      'metrics' => function (HasMany $query)
                                                      {
                                                          $query->orderBy('date', 'desc')
                                                                ->limit(1)
                                                                ->get([
                                                                    '_id',
                                                                    'date',
                                                                    'metric1',
                                                                    'metric2',
                                                                ]);
                                                      },
                                                  ])
                                                  ->get([
                                                      'id',
                                                      'name'
                                                  ]);

I also tried to use first() instead of limit(1)->get(), but that led to the same result.

Expected behaviour

It should return all customers with their latest metrics, similar to this:

"customers": [
        {
            "id": 1,
            "name": "customer 1",
            "metrics":[
                {
                    "_id": "6304a45314e6ad8aff662a13",
                    "date": "2022-08-23T00:00:00.000000Z",
                    "metric1": 81,
                    "metric2": 21
                }
            ]
        },
        {
            "id": 2,
            "name": "customer 3",
            "url": "https://stevie.url.com",
            "metrics": [
                {
                    "_id": "6304a45314e6ad8aff662a12",
                    "date": "2022-08-23T00:00:00.000000Z",
                    "metric1": 33,
                    "metric2": 7
                }
            ]
        },
		...
    ]

Actual behaviour

Only the newest metric of all customers is returned, which results, that most customers have empty metrics:

"customers": [
        {
            "id": 1,
            "name": "customer 1",
            "metrics": []
        },
        {
            "id": 2,
            "name": "customer 3",
             "url": "https://stevie.url.com",
            "metrics": [
                {
                    "_id": "6304a45314e6ad8aff662a12",
                    "date": "2022-08-23T00:00:00.000000Z",
                    "metric1": 33,
                    "metric2": 7
                }
            ]
        },
		...
    ]

@jenssegers
seems to be the same problem as this issue: #1042 (comment)

@divine
Copy link
Contributor

divine commented Aug 23, 2022

Instead of necroposting next time create a new issue.

@stefanschindler
Copy link
Author

@divine didn't I open a new issue? Maybe the problem is already known, but the other issue was closed in 2018 without any reason and it still doesn't seem to work.

@JeRabix
Copy link

JeRabix commented Sep 13, 2022

+1

@Iscrimou
Copy link

Same problem here, Message::with('user') works well, but User::with('messages') came a empty array, and I didn't find any solution in others posts

@eslamismail
Copy link

I have the same issue with laravel 8 and jenssegers/mongodb 3.8.x
it works only with belongsTo relation
hasone or has many return empty response cause the foreign key and primary id is objectID while package search in collection with string type

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants