The solution you provided is already quite elegant. However, if you're looking for an alternative approach, you can use the when() method in combination with the with() method to conditionally load the physicalRelation relation. Here's an example:
$product->load([
'relationX',
'relationY',
])->when(!$product->is_virtual, function ($query) {
$query->with(['physicalRelation' => function ($query) {
$query->orderBy('some_field', 'asc');
}]);
});
This approach allows you to conditionally load the physicalRelation relation based on the value of $product->is_virtual. If it's true, the physicalRelation relation won't be loaded. Otherwise, it will be loaded with the specified ordering.
Note that the with() method is used instead of load() within the when() callback, as load() is not available on the query builder instance returned by when().