-1

I want to use ajax search in my laravel project but I am facing this problem when I am searching this.I am uploading my code please can you tell me where's the problem. This is my controller code:

   public function search(Request $request)
    {
      if($request->ajax())
      {
          $output="";
          $results = DB::table('SaveStudentResult')->where('reg_number','LIKE','%'.$request->reg_id.'%')-get();

          if($results)
          {
              foreach ($results as $key => $result)
              {
                  $output.='<tr>'.
                                    '<td>'.$result->id.'</td>'.
                                    '<td>'.$result->reg_number.'</td>'.
                                    '<td>'.$result->st_name.'</td>'.
                                    '<td>'.$result->email_ad.'</td>'.
                                    '<td>'.$result->department_na .'</td>'.
                                    '<td>'.$result->grade.'</td>'.
                                 '</tr>';
              }
              return Response($output);
          }
      }
    }
And Here is my javascript code

  <script type="text/javascript">
      $('#reg_id').on('keyup',function(){
            $value=$(this).val();
            $.ajax({
                type : 'get',
                url    : '{{URL::to('index')}}',
                data :  {'index':$value},
                success:function(data)
                {
                    console.log(data);
                }
            });
      })
  </script>
KaziBablu
  • 493
  • 1
  • 6
  • 16

1 Answers1

1

Right laravel its expecting a reg_id on the request object and your ajax call its not providing it. So adjust the ajax call to:

$.ajax({

     var data = $("#reg_id").data().value;

      type : 'GET',
      url    : '{{URL::to('index')}}',
      data: {'reg_id': data},
      datatype: 'JSON',
      success: function (response) {

      },
      error: function (response) {

      }
}); 

Add use DB; at the top of your controller. And your query has a typo at the end should be ->get();

And for the sake of clean code thats clear off some of your method:

public function search(Request $request)
{

    if(!$request->wantsJson()){
        abort(403);
    }

    $regId = json_decode($request->input('reg_id'));

    if(empty($regId)){
        return response('Bad request!', 400 );
    }

    $searchResults = DB::table('SaveStudentResult')->where('reg_number','LIKE','%'.$regId.'%')->get();

    if($searchResults->count() == 0){
        return response('No results found', 200);
    }

    return response(

        $searchResults->map(function($result){

         $output.='<tr>'.
                        '<td>'.$result->id.'</td>'.
                        '<td>'.$result->reg_number.'</td>'.
                        '<td>'.$result->st_name.'</td>'.
                        '<td>'.$result->email_ad.'</td>'.
                        '<td>'.$result->department_na   .'</td>'.
                        '<td>'.$result->grade.'</td>'.
                   '</tr>';

         return $output;
        })

    );

  }
Leo
  • 7,274
  • 5
  • 26
  • 48