0

I tried to sort a different kind of status in a model and paginate them in one collection. Is there a way to do this? The sorting will be overridden when I place the sorting condition after the union method.

 ->with(['user']) "eloquent relation"
 ->withPaidAt() "scope in lesson to check if lessons is paid"

$lessons = new Lesson();
$upcomingLessons = $lessons
    ->with(['user'])
    ->withPaidAt()
    ->whereNull('online_starts_at')
    ->orderBy('start_at');

$availableLessons = $lessons
    ->with(['user'])
    ->withPaidAt()
    ->whereNotNull('online_starts_at')
    ->where('visibility', 'public')
    ->orderBy('online_starts_at')
    ->union($upcomingLessons)
    ->paginate(10);

return $availableLessons;

Already checked this Sorting UNION queries with Laravel 4.1 where

$query1->orderBy('foo desc');
$query2->orderBy('foo desc');
$query1->union($query2);

is not working.

Yadda Yadda
  • 1
  • 1
  • 1
  • Can you post your ["minimal reproducible example"](https://stackoverflow.com/help/minimal-reproducible-example) code, that way we can write answers that apply to the code you're actually working with? This should make the answers more easily transferable to your environment. As well as making the answers more useful to future visitors. – steven7mwesigwa Mar 17 '22 at 00:33
  • thanks @steven7mwesigwa already updated the code and provide some details on it. – Yadda Yadda Mar 17 '22 at 08:16

0 Answers0