Quick and Dirty
First, get your users eager loading their properties' dates and all the offers.
'property'=>function($query){
$query->select('contract_startdate','otherdate');
'offer'=>function($query){
Assuming you've properly set the $dates
array in your model to include your contract_startdate
and other_date
. We can use carbon to filter the collection to get the properties we're interested in counting. In your view, you can:
<th>Property (start date)</th>
<th>Property (other date)</th>
@foreach($users as $user)
->filter(function($item) use ($filter_start,$filter_end){
return $item->contract_startdate->between($filter_start,$filter_end);
->filter(function($item) use ($filter_start,$filter_end){
return return $item->other_date->between($filter_start,$filter_end);
<td>{{$user->offers->count()}}</td>
Cleaning that up
You should likely refactor the filter out of the view for cleanness, but doing so will add another loop over collection. But you might be able to remove a loop by doing something like this in your controller.
'property'=>function($query){
$query->select('contract_startdate','otherdate');
'offer'=>function($query){
$byContractDate = collect();
$byOtherDate = collect();
foreach($users as $user){
foreach($properties as $property){
if($propery->contract_startdate->between($filter_start,$filter_end){
if($propery->contract_startdate->between($filter_start,$filter_end){
$byContractDate->put($user->id,$contractCounter);
$byOther->put($user->id,$otherCounter);
And in your view:
<th>Property (start date)</th>
<th>Property (other date)</th>
@foreach($users as $user)
<td>{{$byContract->get($user->id)}}</td>
<td>{{$byOther->get($user->id)}}</td>
<td>{{$user->offers->count()}}</td>