firstname
lastname
DOB
gender
status (Active, Not Active)
fee_paid
I also have a search form on the views that I want to use to search students based on different conditions.
For example, I may want to search these
All female students who were born between date1 to date2 and have paid x amount of fees.
All students called Ali who have paid x amount of fees.
All Active students who have not paid any fees.
My question, how can I simply check, if a field is set, then use it in a query, otherwise don't include it in the query. Also for DOB, search is by date range.
// make a base query
$query = Model::all();
// check if form has value
if (Input::has('firstname')) {
// modify the query
$query->where('firstname', Input::has('firstname')); // only if firstname = input_value
}
// pull the query
$result = $query->get();
Thanks @TerrePorter, but when the user doesn't specify the search the criteria, I get Missing argument 1 for Illuminate\Support\Collection::get() error
And then have different methods for each query type:
public function queryGender($query, $gender)
{
$query->where('gender', $gender);
}
It might be a little cleaner than having a huge conditional to check for all the input values and you could reuse the code in the query methods for other things.