You can use the withCount method to get the count of related models. In this case, you can use the withCount method on the Transaction model to get the count of related TransactionLog models. You can then use the whereHas method to filter the results based on the gateway_id and then use the orderBy method to order the results by the count of related TransactionLog models. Finally, you can use the get method to get the results.
$provinces = Transaction::withCount('transactionLog')
->whereHas('transactionLog', function ($query) use ($gateway_id) {
$query->where('gateway_id', $gateway_id);
})
->orderBy('transaction_log_count', 'desc')
->get();