Last active
August 29, 2015 14:27
-
-
Save helmerdavila/6903790856a922a70b69 to your computer and use it in GitHub Desktop.
Collection Filter and Merge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function list(Request $request) | |
{ | |
$filtering = new Collection; | |
Expenditure::with('type', 'concept', 'store') | |
->chunk(200, function ($listExpenditures) use (&$filtering, $request) { | |
if ($request->has('startDateRange') && $request->has('endDateRange')) { | |
$startDate = Carbon::createFromFormat('Y-m-d', $request->input('startDateRange')); | |
$endDate = Carbon::createFromFormat('Y-m-d', $request->input('endDateRange')); | |
$listExpenditures = $listExpenditures->filter(function ($expenditure) use ($startDate, $endDate) { | |
if ($expenditure->emited->between($startDate, $endDate)) { | |
return true; | |
} | |
}); | |
} | |
if ($request->has('type')) { | |
$listExpenditures = $listExpenditures->filter(function ($expenditure) use ($request) { | |
if ($expenditure->type_id == $request->input('type')) { | |
return true; | |
} | |
}); | |
} | |
if ($request->has('store')) { | |
$listExpenditures = $listExpenditures->filter(function ($expenditure) use ($request) { | |
if ($expenditure->store_id == $request->input('store')) { | |
return true; | |
} | |
}); | |
} | |
if ($request->has('concept')) { | |
$listExpenditures = $listExpenditures->filter(function ($expenditure) use ($request) { | |
if ($expenditure->concept_id == $request->input('concept')) { | |
return true; | |
} | |
}); | |
} | |
$filtering = $filtering->merge($listExpenditures); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment