It doesn't make any sense to me but I think you may try this to get the $query
instance:
$users = User::with(array('posts' => function($query) {
$query->orderBy('created_at', 'desc');
}));
$relation = $users->getRelation('posts');
The $relation
will contain an instance of Illuminate\Database\Eloquent\Relations\HasMany
(hopefully) but not sure how you use it but you may use $relation->getForeignKey()
to get the foreignkey
property. Finally, $users->get()
will execute the query.
You may also get the relation ($query) object using something like this:
$user = User::query();
$relation = $user->getRelation('posts');
dd($relation); // Illuminate\Database\Eloquent\Relations\HasMany
P/S: It's not clear to me what you are going to do so if it helps and you get what you wanted to do then please share the idea/thing in comment. Thanks!