Sort by Laravel polymorphic relationship column

Asked

Viewed 24 times

1

I need to sort the results by column distance relationship between order and address by paging the results.

Order:

public function address()
{
    return $this->morphOne(Address::class, 'addressable');
}

Address:

public function addressable()
{
    return $this->morphTo();
}

I tried the way down but didn’t order:

$orders = Order::with(['address' => function ($query) {
    $query->orderBy('distance', 'asc');
}])->paginate(15)
  • Tries $query->orderBy('address.distance', 'asc');

  • Unknown column 'address.Distance'

1 answer

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.