Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

4xjbh's avatar
Level 4

Select2 response fail

I am having trouble getting this to make a response and I am not sure why. The select2 control seems to respod correctly but the function seems to be the problem....I think.

Any assistance would be appreciated.

ROUTE

Route::get('/api/selectcompany', 'CompanyController@selectcompany');

CONTROLLER

public function selectcompany(Request $request)
    {
        $term = trim($request->q);
        if (empty($term)) {
            return \Response::json();
        }
        $companies = Company::where('CompanyName', '%LIKE%', '%'.$term.'%')->paginate(20);
        $formatted = [];
        foreach ($companies as $company) {
            $formatted[] = ['id' => $company->ID, 'text' => $company->CompanyName];
        }
        return \Response::json($formatted);
    }

SCRIPT

  <script>
      $(document).ready(function() {
          $('#companyid').select2({
              placeholder: "Search for a company...",
              minimumInputLength: 3,
              ajax : {
                  url : '/api/selectcompany',
                  dataType : 'json',
                  delay : 200,
                  data : function(params){
                      return {
                          q : params.term,
                          page : params.page
                      };
                  },
                  processResults : function(data, params){
                      params.page = params.page || 1;
                      return {
                          results : data.data,
                          pagination: {
                              more : (params.page  * 10) < data.total
                          }
                      };
                  }
              }
          });
      });
  </script>
0 likes
4 replies
tykus's avatar
tykus
Best Answer
Level 104

Not '%LIKE%', just 'LIKE' the wildcard wrap the search term, not the operator.

Also, you are not returning any of the Paginator data in the response, so there is no way for the additional requests to fire - there is no data.total to evaluate if there are more results! You would need to replace the Paginator's items - you could return the result of $companies->hasMorePages() in the response, or replace the items in the Paginator with the $formatted contents using setItems()

4xjbh's avatar
Level 4

Is there a better way to do what I am trying to do?

tykus's avatar

What you are doing is perfectly fine in my opinion... you simply need to given select2 the data it needs.

4xjbh's avatar
Level 4

@tykus Thank you for you help. I went back to basics and read through the select2 documentation and used your hints .

CONTROLLER

public function selectcompany(Request $request)
    {
        $term = trim($request->q);
        if (empty($term)) {
            return response()->json();
        }
        $companies = Company::where('CompanyName', 'LIKE', '%'.$term.'%')->orderby('CompanyName')->paginate();
        $morePages = $companies->hasMorePages();
        $formatted = [];
        foreach ($companies as $company) {
            $formatted[] = ['id' => $company->ID, 'text' => $company->CompanyName];
        }
        $results = array(
            "results" => $formatted,
            "pagination" => array(
                "more" => $morePages
            )
        );
        return response()->json($results);
    }

SCRIPT

 <script>
      $(document).ready(function() {
          $('#companyid').select2({
              placeholder: "Search for a company...",
              minimumInputLength: 3,
              ajax : {
                  url : '/api/selectcompany',
                  dataType : 'json',
                  delay : 250,
                  data : function(params){
                      return {
                          q : params.term,
                          page : params.page
                      };
                  },
                  processResults : function(data, params){
                      params.page = params.page || 1;
                      return {
                          results : data.results,
                          pagination: {
                              more : data.pagination.more
                          }
                      };
                  }
              }
          });
      });
  </script>

Please or to participate in this conversation.