Skip to content

Instantly share code, notes, and snippets.

@helmerdavila
Last active August 29, 2015 14:27
Show Gist options
  • Save helmerdavila/6903790856a922a70b69 to your computer and use it in GitHub Desktop.
Save helmerdavila/6903790856a922a70b69 to your computer and use it in GitHub Desktop.
Collection Filter and Merge
<?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