0

I'm looking for the distance around a particular point.

I have these items in my db with a lat and long.

I want to get all the results around it for a specified radius.

This is how i get it:

    $lat = '51,3';
    $long = '4,3';
    $radius = '5';

    $hamsters = Hamster::select(
           DB::raw("*,
                         ( 6371 * acos( cos( radians(?) ) *
                           cos( radians( lat ) )
                           * cos( radians( long ) - radians(?)
                           ) + sin( radians(?) ) *
                           sin( radians( lat ) ) )
                         ) AS distance"))
           ->having("distance", "<", "?")
           ->orderBy("distance")
           ->setBindings([$lat, $long, $lat,  $radius])
           ->get();

Now it gives me an error:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'long ) - radians(?) ) + sin( radians(?) ) * ' at line 4 (SQL: select *, ( 6371 * acos( cos( radians(51,3) ) * cos( radians( lat ) ) * cos( radians( long ) - radians(4,3) ) + sin( radians(51,3) ) * sin( radians( lat ) ) ) ) AS distance from hamsters having distance < 5 order by distance asc)

Anybody a way to fix this?

1
  • Why dont you ->toSql() instead of ->get() and echo it and see the query that is being generated. Commented Nov 11, 2014 at 19:46

1 Answer 1

2

Long is a reserved keyword in mysql source

You'd have to use backticks (`) around any labels that share the same name as a workaround.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.