EagerLoading doesn't work on helpers


#1

EagerLoading doesn’t work on helpers methods
This one works fine:

await user.query()
        .where('enabled', 1)
        .with('Posts')
        .orderBy('some_time_column', 'DESC')
        .limit(1)
        .fetch();

But when I try with helpers such (first() ) it won’t work:

await user.query()
        .where('enabled', 1)
        .with('Posts')
        .orderBy('some_time_column', 'DESC')
        .first();

With the first query I get back the “Posts” object, but the second does not return “Posts”.

I don’t really care using the fetch method, just wanted to know if it should behave like this?

Thanks!


#2

Hi, could you try the following

await User.query().where('enabled', 1).with('Posts').orderBy('column', desc).find(params.id)

Best Regards


#3

Hi IngPaz

But if I don’t have the “id” of the user?
Or if it’s not even a users table but something else…


#4

you can pass the id of the table your querying


#5

I understand, but I’ve asked on case which I don’t have the id of the row.
Only some parameters like “enabled” and orderby date.
In that case “find()” won’t help me.


#6

How do you check the existence of posts?


#7

Hi Virk,

There could be a case where there’s no posts related to user
well in that case the post will return empty array