Using a relationship SUM in a where statement

Sunday, May 11, 2014

Struggling to get my head around this one, I am trying to get the SUM if a relationships column in a where clause to see if there is enough space to put a new service.

$nodes = Node::where(function($q) {
$q->where('location_id', '=', $location);

$q->where('WHAT TO PUT HERE....')

if($this->dedicated_ip) {
$q->whereHas('ips', function($q){
$q->where('assigned_to', '<=', 1);
$q->where('assigned_to', '>=', -1);

I am wanting to do something like:

WHERE ( $this->stress_points + Node->used() < nodes.stress_points )

the used function is just:

public function used() {
return $this->hasMany('Server', 'node_id', 'id')->sum('stress_points');