Laravel 对多个 withCount 结果求和
Laravel sum multiple withCount results
我想按多个 whereCount 结果的总和进行排序。这是我的 whereCount
$q->withCount(['ordered', 'favorites']);
我想对这些 ordered_count+favorites_count
求和并按此结果排序。我不太擅长使用 SQL 所以我需要一些帮助。如果使用 eloquent 查询生成器可以做到这一点,那甚至 better.Thank 你也是。
您可以使用orderByRaw按总和排序:
$q->withCount(['ordered', 'favorites'])
->orderByRaw('(ordered_count + favorites_count) desc')
->get();
我想按多个 whereCount 结果的总和进行排序。这是我的 whereCount
$q->withCount(['ordered', 'favorites']);
我想对这些 ordered_count+favorites_count
求和并按此结果排序。我不太擅长使用 SQL 所以我需要一些帮助。如果使用 eloquent 查询生成器可以做到这一点,那甚至 better.Thank 你也是。
您可以使用orderByRaw按总和排序:
$q->withCount(['ordered', 'favorites'])
->orderByRaw('(ordered_count + favorites_count) desc')
->get();