ok, a few first steps to writing better code:
public function searchCases(array $searchData)
{
$from = $searchData['from'];
$to = $searchData['to'];
$district = $searchData['district'];
[...]
}
What is happening here is: You created a method (searchCases) that accepts an array of data, but you have to read through the whole method to see which keys are needed in this array.... This is going to bite you later (as you see now with the Undefined index: distinct).
Reformat that method into:
public function searchCases($from, $to, $district = NULL)
{
[...]
}
Now you instantly see that the method needs a $from and $to and has an optional $district parameter and we can remove the first 3 lines of the method which try to extract that data from the array (without even checking if the keys existed in the first place, giving you that undefined index error)
Now for the actual code:
public function searchCases($from, $to, $district = NULL)
{
$cases = Case::whereDate('created_at', '>=', $from)
->whereDate('created_at', '<=', $to);
if($district) {
$cases = $cases->where('district',$district);
}
return $cases->paginate(1);
}
We start building a new query, based only on the $from and $to dates. If a district was given, we'll apply that as well.
And we return the paginated results.
Using the repository would now be a matter of:
$cases = $this->case->searchCases($request->get('from'), $request->get('to'), $request->get('district'));