0

I want to add the option when 'recommendation' or 'source' is selected from dropdown, to get all Candidates which have in one of those two columns something other than null or '0', and sort them using 'created_at', from-to date.

This is my view:

    <div class="container">
<div class="row">
<div class="col-lg-12 col-md-12">
    <form class="search-form search-form-basic" action="/candidates/index" method="post">
    {{ csrf_field() }}
        <div class="form-row">              
            <div class="col-md-4 form-group">
                <label for="search_email">First name:</label>
                <input type="text" name="search_first_name" id="search_first_name" class="form-control" @if(isset(Session::get('inputs')['search_first_name'])) value="{{ Session::get('inputs')['search_first_name'] }}" @endif>
            </div> 
            <div class="col-md-4 form-group">
                <label for="search_last_name">Last name:</label>
                <input type="text" name="search_last_name" id="search_last_name" class="form-control" @if(isset(Session::get('inputs')['search_last_name'])) value="{{ Session::get('inputs')['search_last_name'] }}" @endif>
            </div> 
            <div class="col-md-4 form-group">
                <label for="search_round_number">Round Number:</label>
                <input type="text" name="search_round_number" id="search_round_number" class="form-control" placeholder="" @if(isset(Session::get('inputs')['search_round_number'])) value="{{ Session::get('inputs')['search_round_number'] }}" @endif>
            </div>  
            <div class="col-md-4 form-group">
                <label for="search_location">location:</label>
                <input type="text" name="search_location" id="search_location" class="form-control" 
                 @if(isset(Session::get('inputs')['search_location'])) value="{{ Session::get('inputs')['search_location'] }}" @endif>
            </div> 
            <select name="options" class="col-md-4 form-group">
             <option value="">--- Select From ---</option>
              <option  value="birth_date">Birth Date</option>
              <option  value="CV_grade_date">CV Grade Date</option>
              <option  value="source">Source</option>
              <option  value="recommendation">Recommended</option>                
              <option  value="created_at">Applied</option>
            </select>
            <div class="col-md-4 form-group">
                <label for="search_from_date">From:</label>
                <input type="date" name="search_from_date" id="search_from_date" class="form-control" 
                 @if(isset(Session::get('inputs')['search_from_date'])) value="{{ Session::get('inputs')['search_from_date'] }}" @endif>
            </div> 
            <div class="col-md-4 form-group">
                <label for="search_to_date">To:</label>
                <input type="date" name="search_to_date" id="search_to_date" class="form-control" 
                 @if(isset(Session::get('inputs')['search_to_date'])) value="{{ Session::get('inputs')['search_to_date'] }}" @endif>
            </div>  
        </div>
        <div class="form-row">
            <div class="col-md-12 col-lg-12">
                <button type="submit" class="btn btn-custom"><i class="fa fa-search" aria-hidden="true"></i>Search</button>
                <a href="/users" class="btn btn-custom"><i class="fa fa-times-circle" aria-hidden="true"></i>Clear</a>
            </div>
        </div>
    </form>
</div>

This is my Controller, where i tried to implement adding a new variable with value of either 'recommendation' or 'source', assign that value to a new variable, and then drop the $options variable, so I skip

        ->when($options, function ($query) use ($options,$search_from_date,$search_to_date  ) {
           return $query->whereBetween($options, array($search_from_date, $search_to_date));

Here is my code:

public function index(Request $request) {

    if($request->isMethod('post')){
    $search_first_name =    $request->search_first_name;
    $search_last_name =     $request->search_last_name;
    $search_email =         $request->search_email;
    $search_round_number =  $request->search_round_number;
    $search_location =      $request->search_location;

    $options = Input::get('dropdown');

    if($options == 'recommendation' || 'source')   {
            $recOrSource = Input::get('options');
            unset($options);  
    }

    $search_from_date =      $request->search_from_date;
    $search_to_date =      $request->search_to_date;

    $candidate = DB::table('candidates')
                ->when($search_first_name, function ($query) use ($search_first_name) {
                    return $query->where('first_name', 'like', '%' . $search_first_name . '%');
                })
                ->when($search_last_name, function ($query) use ($search_last_name) {
                    return $query->where('last_name', 'like', '%' . $search_last_name . '%');
                })
                ->when($search_email, function ($query) use ($search_email) {
                    return $query->where('email', $search_email);
                })
                ->when($search_round_number, function ($query) use ($search_round_number) {
                    return $query->where('round_number', $search_round_number);
                })
                ->when($search_location, function ($query) use ($search_location) {
                   return $query->where('location', $search_location);
                })                       
                ->when($options, function ($query) use ($options,$search_from_date,$search_to_date  ) {
                   return $query->whereBetween($options, array($search_from_date, $search_to_date));
                })    
                ->when($recOrSource, function ($query) use ($recOrSource,$search_from_date,$search_to_date  ) {
                   return $query->where(!empty($recOrSource))->whereBetween('created_at', array($search_from_date, $search_to_date));
                })                                                                                     
                ->orderBy('first_name', 'asc')
                ->orderBy('last_name', 'asc')
                ->get();                     

                Session::flash('inputs', [
                    'search_first_name' => $search_first_name,
                    'search_last_name' => $search_last_name,
                    'search_email' => $search_email,
                    'search_round_number' => $search_round_number,
                    'search_from_date' => $search_from_date,
                    'search_to_date' => $search_to_date,
                    'search_location' => $search_location

                    ]);
            }else{
                 Session::forget('inputs');


    $candidate = Candidate::orderBy('first_name', 'asc')
                    ->orderBy('last_name', 'asc')
                    ->get();
                }

    return view('/candidates/index', [
        'candidate' => $candidate
    ]);

}

The error I get is "Undefined variable: options", However I didn't find a way to resolve this. And I did see similar questions but nothing was working for me.

Nick
  • 181
  • 1
  • 13
  • Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – miken32 Nov 09 '18 at 21:08

1 Answers1

0

1) In your view the <select> name attribute is options, but in your controller you are retrieving the value of dropdown $options = Input::get('dropdown');. It should be:

$options = Input::get('options');

2) If you want check if the selected option is recommendation or source, your if statement should be:

if ($options == 'recommendation' || $options == 'source')

3) Inside your if statement you are unsetting $options variable, but lines below you have when($option,.., then you should change unset($options); to:

$options = null;

otherwise it will throw "Undefined variable: options" again.

4) Also, in your controller you using ->when($recOrSource,.., but the variable $recOrSource is declared inside your if statement, so If you don't select "recommended" or "source", you will get "Undefined variable: recOrSource". To fix that you should declare $recOrSource i.e. outside your if statement.

$recOrSource = null;

if ($options == 'recommendation' || $options == 'source') {

Wrapping up, the changes you should do in your controller:

...
$options = Input::get('options');
$recOrSource = null;

if($options == 'recommendation' || $options == 'source')   {
    $recOrSource = Input::get('options');
    $options = null;
}
...
Erick Patrick
  • 441
  • 3
  • 5