0

I'm running this query filtering relation entries. It's working good.

$places = Place::
                with([
                  'mytickets' => function($q) use ($from, $to) {
                    $q->whereBetween('used_at', [$from, $to]);
                  }])
                ->get();

So now I want to get results sorting DESC by the count of mytickets.

Is it possible?

1 Answer 1

1

Yes, it's possible. Try something like : (i haven't tested)

$places = Place::with(['mytickets' => function($q) use ($from, $to) {
                 $q->whereBetween('used_at', [$from, $to]);
               }
          ])
          ->get()
          ->sortBy(function($places){
               return $places->mytickets->count();
          });
Sign up to request clarification or add additional context in comments.

2 Comments

it worked but it gets ASC. is there any way to make it DESC?
yes, use sortByDesc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.