I have the following PHP code:
$users = DB::table('users');
if (Input::has('minAge') && Input::has('maxAge')) {
$users = $users->whereBetween('age', array(Input::get('minAge'), Input::get('maxAge')));
} else if (Input::has('minAge')) {
$users = $users->where('age', '>=', Input::get('minAge'));
} else if (Input::has('maxAge')) {
$users = $users->where('age', '>=', Input::get('maxAge'));
}
if (Input::has('sex') && Input::get('sex') !== "0") {
$users = $users->where('sex', '=', Input::get('sex'));
}
$users = $users->paginate(15);
return View::make('search.result', compact('users'));
However I would like to retrieve through Eloquent as the entity object has some functionality needed in view.
Anyway, I've tried to mimic the above but it doesn't work, I think the objects are rather overriden than chained.
Here is my attempt:
if (Input::has('minAge') && Input::has('maxAge')) {
$users = User::whereBetween('age', array(Input::get('minAge'), Input::get('maxAge')));
} else if (Input::has('minAge')) {
$users = User::where('age', '>=', Input::get('minAge'));
} else if (Input::has('maxAge')) {
$users = User::where('age', '>=', Input::get('maxAge'));
}
if (Input::has('sex') && Input::get('sex') !== "0") {
$users = User::where('sex', '=', Input::get('sex'));
}
$users = User::paginate(15);
return View::make('search.result', compact('users'));