Thursday, March 2, 2017

Ajax call returns 500 if controller queries database without ->take(); other wise 200

I've written a google maps page for a client which includes a draw search feature written from scratch, as in draw a polygon to return locations within the polygon.

Everything works fine on the localhost.

Upon completion of the polygon (drag the final segment to the original start point) ajax calls a function that searches the database with the same DB query used in a normal non-polygon search. However, I can't get the dam thing to work without include a ->take() parameter. This makes no sense to me at all.

Controller- I've taken out some unnecessary parts of code to make it easier to read but you can see it's a regular old db query. If I replace the ->orderBy here with a ->take(some number less than or equal to total) it works fine.

$searchResults = DB::table('properties')->leftJoin('propImages',   
'properties.L_ListingID', '=', 'propImages.L_ListingID')
->where($propertiesArray)
->whereIn('LM_Char10_1', $waterArray)
->whereIn('L_Class', $propTypes)
->when($residential==1, function($query) use ($bed, $bath) {// when residental is true, get beds/bath criteria return
$query->where([['L_Keyword3', '>=', $bed],['LM_Int1_1', '>=',
$bath]]); }); 
$sr = $searchResults->orderBy('L_AskingPrice',
'asc')->get();

view

google.maps.event.addListener(drawingManager, 'polygoncomplete', function (polygon) {

                        console.log('polygon complete');
                        $('#overMapLoadIcon').css('display', 'inline');
                        //Hide drawl button
                        $('#deletePoly').css('display', 'inline');
                        $('#updatePolySearch').css('display', 'inline');
                        drawingManager.setOptions({
                          drawingControl: false
                        });
                        //Set draw mode to hand
                        drawingManager.set('drawingMode');

                        polyPoints = [];// create an arry of all the point's GPS coords in the polygon by looping through them
                          for (var i = 0; i < polygon.getPath().getLength(); i++) {//For Each Polygon point
                              var poylygonCoord = polygon.getPath().getAt(i).toUrlValue(6);

                              var coords = poylygonCoord.split(",");
                              var lat = coords[0];
                              var lng = coords[1];
                              polyPoints.push([lat,lng]);
                              //console.log(lat+'    '+lng);
                          }

                          // with array of pollygon points coordinates, send to controller with search criteria
                          var waterType = $('#typeOfWaterfront').val();
                          var priceSlider = $('#priceSlider').val();
                          var footageSlider = $('#footageSlider').val();
                          var beds = $("body").find("[aria-selected=true]").parent().attr("data-original-index");
                          var baths = $("body").find("[aria-selected=true]").parents().attr("data-original-index");
                          var residential = $('[name*="residential"]:checkbox:checked').length > 0;
                          var vacantland = $('[name*="vacantland"]:checkbox:checked').length > 0;
                          var commercial = $('[name*="commercial"]:checkbox:checked').length > 0;
                          var condos = $('[name*="condos"]:checkbox:checked').length > 0;
                          //$("body").find("[aria-selected=true]").parents().attr("data-original-index");

                          var polyPointsString = JSON.stringify(polyPoints);
                          var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
                          $.ajax({
                              url: '<?php echo URL::to("/searchByPoly/") ?>',
                              type: 'POST',

                              data: {_token: CSRF_TOKEN, polyPoints: polyPointsString,
                               waterFrontType: waterType,
                               price: priceSlider,
                               footage: footageSlider,
                               bed: beds,
                               bath: baths,
                               res: residential,
                               vacant: vacantland,
                               comm: commercial,
                               condo: condos },
                              success: function (data) {
                                jsonProps = JSON.parse(data);
                                $('#overMapLoadIcon').css('display', 'none');
                                removeAllMarkers();
                                markers=[];

                                for (var i = 0; i < jsonProps.length; i++) {
                                  //L_DisplayId
                                  placeMarkerFromPoly(jsonProps[i]);  
                                }

                              }
                          });

This can't be a token issue since I can still get the ajax to work. I'm stupped. Any help would be greatly appreciated.



via Elliot Robert

Advertisement