-
Notifications
You must be signed in to change notification settings - Fork 28
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 with SoftDeletes (inside with() lambda) #56
Comments
Do you mean this? auto user = User::withTrashed()->with(...).get() or something like this should be possible too, but it's doing something different than above and the following it's not eager load: user->posts()->onlyTrashed().get();
user->posts()->withTrashed().get(); |
I want something like this user->posts()->withTrashed().get(); But I have ModelsCollection and I haven't found how i can do this. P.S. auto user = User::withTrashed()->with({{"posts", [](auto& query){query.whereRaw(R"(1 = 1 --)")}}}).get() this sql injection helps me to disable |
UPD |
So you have ModelsCollection of Users and you need to select all Posts that are not trashed eagerly. You have two choices, this is what I came up with fastly: auto posts = Post::withTrashed()->whereIn("user_id", users.modelKeys()).get(); or auto users = User::with({{"posts", [](auto &query)
{
query.whereNotNull("deleted_at");
}}})->get(); |
Thanks! |
Another solution would be to define the relation like: std::unique_ptr<HasMany<User, Post>>
postsTrashed()
{
auto relation = hasMany<Post>(u"post_id"_s);
relation->withTrashed();
return relation;
} |
The real problem here is that you should be able to call it like this: auto users = User::with({{"posts", [](auto &query)
{
query.withTrashed();
}}})->get(); But this is currently not possible because of this, the relation reference should be passed into this lambda (eg. in this case And if not a relation reference then at least It would be a valuable refactor. Maybe I should try fixing it. It's not a big deal though. For example, in this case, you can write the thing manually, like |
I leave it open as it can be enhanced in the future. |
Hi
Is there any way i can load relation with trashed items in
with
method?The text was updated successfully, but these errors were encountered: