0

what i'm trying to do is to Sum the total deposits of each reservation model, with the condition of less than the amount input in the text.

here's my query:

$reservations->whereHas('deposits', function($query) use ($etc_filters){
    $query->havingRaw('SUM(amount) <= '.$etc_filters);
});

as you can see, i'm using havingRaw that can be injected with another query. right now i cant find any alternative solution for my code.

Douwe de Haan
  • 6,247
  • 1
  • 30
  • 45
jp.palubs
  • 178
  • 1
  • 14

1 Answers1

2

You can use the second argument the havingRaw method accepts, to make the value a binding, which gets escaped before it is inserted in the query:

$reservations->whereHas('deposits', function($query) use ($etc_filters){
    $query->havingRaw('SUM(amount) <= ?', $etc_filters);
});
Douwe de Haan
  • 6,247
  • 1
  • 30
  • 45