Click here to Skip to main content
15,891,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<script type="text/javascript">
        // CSRF Token
        var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
        $(document).ready(function() {

            $('#sel_city').select2({
                placeholder: "Selecteaza Oras",
                ajax: {
                    url: "{{route('getCities')}}",
                    type: 'POST',
                    dataType: 'json',
                    delay: 250,
                    data: function(params) {
                        return {
                            _token: CSRF_TOKEN,
                            search: params.term // search term
                        };
                    },
                    processResults: function(response) {
                        return {
                            
                            results: response
                        };
                    },
                    cache: true
                }

            });

        });
    </script>


in web i have:
Route::post('/getcities', [Controllers\ListingController::class, 'getCities'])
    ->name('getCities');
//end cities


in Controller
use App\Models\Cities;

and

public function getCities(Request $request)
   {
       $search = $request->search;

       if ($search == '' ) {
           $cities = Cities::orderBy('city_name', 'asc')
               ->select('id', 'city_name')
               ->limit(5)
               ->get();
       }else{
           $cities = Cities::orderBy('city_name','asc')
           ->select('id','city_name')
           ->where('city_name', 'like', '%' .$search. '%')
           ->limit(5)
           ->get();

       }

       $response = array();

       foreach ($cities as $city) {
           $response[] = array(
               'id' => $city->id,
               'text' => $city->city_name
           );
       }
       return response()->json($request);


and in the model only
class Cities extends Model
{
    use HasFactory;
    protected $guarded = [];
   

    
}


What I have tried:

and still not getting info from db, 
mention : i don't have errors in insepct.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900