pavel
pavel

Reputation: 7

Laravel eloquent query + Haversine distance calculator

I have written this query but it is complaining syntax error, unexpected '->' (T_OBJECT_OPERATOR)

at the point where the HAVING statement starts so it must be some apostrophe or semicolon above. I have tried several combinations in how to write the Alias (as distance) with regards to the number of parentheses so that shifted the error down below.

Here is the query:

 $properties  = DB::table('properties')
            ->join('addresses', 'properties.id_address_fk', '=', 'addresses.id')
            ->select('properties.id', 'title', 'city', 'price', 'postedat',
                 ( '3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) )
                       + sin( radians(37) ) * sin( radians( lat ) ) ) as distance') );


            if (!empty($location)) {
            $properties = $properties->where('location', '=', $location);
            }

             if (!empty($propertytype)) {
            $properties = $properties->where('propertytype', '=', $propertytype);
            }

             if (!empty($bedrooms)) {
            $properties = $properties->where('bedrooms', '>=', $bedrooms);
            }

             if (!empty($transaction)) {
            $properties = $properties->where('transaction', '=', $transaction);
            }

             if (!empty($minprice)) {
            $properties = $properties->where('price', '>=', $minprice);
            }

             if (!empty($maxprice)) {
            $properties = $properties->where('price', '<=', $maxprice);
            }


            ->having('distance', '<', $radius)
            ->orderBy('distance', 'desc')
            ->skip(10)
            ->take(5)
            ->get();

Upvotes: 0

Views: 969

Answers (1)

Joseph Silber
Joseph Silber

Reputation: 219936

You have to use the query object:

$properties->having('distance', '<', $radius)

Upvotes: 1

Related Questions