I'm using Laravel 9 to perform a withSum on my relationship credit_transactions. I need to return all PersonalAccessToken models where the credit_balance is less than a certain value, for example, 50 - this way my application knows whether to top up their balance.
This is my query:
/** * Get all keys that can be auto topped-up */ public function getEnabledTopUpKeys() { return PersonalAccessToken::whereNotNull('region_code') ->where('auto_topup_enabled', true) ->whereNotNull('auto_topup_slug') ->withSum('credit_transactions AS credit_balance', 'delta') ->where('credit_balance', '<', 100) ->get(); } This doesn't work, it throws an error:
Unknown column 'credit_balance' in 'where clause'
What am I missing to perform this?