Friday, March 3, 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.

----UPDATE---

The network response is 500. I get nothing back in the form of a preview. There is no laravel error message

My Log file.

Should I be attaching the bottom few lines like so?:

#73 /var/www/upnorthproperties.net/public_html/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(699): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#74 /var/www/upnorthproperties.net/public_html/laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(675): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#75 /var/www/upnorthproperties.net/public_html/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(246): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#76 [internal function]: Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#77 /var/www/upnorthproperties.net/public_html/laravel/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(52): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#78 /var/www/upnorthproperties.net/public_html/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(44): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#79 [internal function]: Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#80 /var/www/upnorthproperties.net/public_html/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(136): call_user_func_array(Array, Array)
#81 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#82 /var/www/upnorthproperties.net/public_html/laravel/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#83 [internal function]: Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#84 /var/www/upnorthproperties.net/public_html/laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#85 /var/www/upnorthproperties.net/public_html/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(132): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#86 /var/www/upnorthproperties.net/public_html/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(99): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#87 /var/www/upnorthproperties.net/public_html/laravel/public/index.php(54): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#88 {main}  



via Elliot Robert

Advertisement