Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ssquare's avatar

How to convert following sql query to laravel query buillder?

$sql = "SELECT id, name, ( 3959 * acos ( cos ( radians(?) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(?) ) + sin ( radians(?) ) * sin( radians( latitude ) ) ) ) AS distance FROM branch HAVING distance < ? ORDER BY distance LIMIT 0 , 20";

$data = \DB::select($sql,[$latitude, $longitude, $latitude, $distance]);


I am trying to get all the branch office which is in the perimeter of a certain distance. The above code is running well with SQL query but I want to try with query builder. Could anyone suggest me its equivalent.

Update:

$query  = \DB::table('branch')
            ->select(
                'id',
                'name',
                \DB::raw('( 3959 * acos ( cos ( radians(?) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(?) ) + sin ( radians(?) ) * sin( radians( latitude ) ) ) ) as distance HAVING distance < ?',$request->distance)
            )
            ->orderBy('distance', 'asc')
            ->offset(0)
            ->limit(20);

        $data = $query->get();

Error

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 'HAVING distance < ? from rinks order by distance asc limit 20 offset 0' at line 1

0 likes
8 replies
ssquare's avatar

@aurawindsurfing I don't find your answer helpful in any direction. Do you think that some people just rush without any sort of searching or going through documentation? You get the notification and rush to check the answers and get reply why don't you google yourself? What is that?

Anyway in case if you are looking for any more attempt how I tried this with a raw query before. Please refer to the question.

ssquare's avatar

@mahaveer I have tried raw queries already but I could not get what I am doing wrong.

kima's avatar

@ssquare accourding to your updated code, your having should be removed from the ->select() clause, to a ->having() clause:

        $query  = DB::table('branch')
            ->select(
                'id',
                'name',
                \DB::raw('( 3959 * acos ( cos ( radians(?) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(?) ) + sin ( radians(?) ) * sin( radians( latitude ) ) ) ) as distance')
            )
            ->having("distance", "<", $request->distance)
            ->orderBy('distance', 'asc')
            ->offset(0)
            ->limit(20);

        $data = $query->get();
ssquare's avatar

@kima As you suggested, I tried with your answers but it showing Invalid parameter number

As there are parameters for latitude, longitude as well I tried with following:

$query  = \DB::table('branch')
            ->select(
                'id',
                'name',
                \DB::raw('( 3959 * acos ( cos ( radians(?) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(?) ) + sin ( radians(?) ) * sin( radians( latitude ) ) ) ) as distance',[$latitude, $longitude, $latitude])
            )
            ->having("distance", "<", $request->distance)
            ->orderBy('distance', 'asc')
            ->offset(0)
            ->limit(20);

        $data = $query->get();

But still, it's saying invalid parameter number.

kima's avatar
kima
Best Answer
Level 2

DB::raw has no $bindings parameter. please try this:

 $query  = DB::table('branch')
            ->select(['id', 'name'])
            ->selectRaw("( 3959 * acos ( cos ( radians(?) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(?) ) + sin ( radians(?) ) * sin( radians( latitude ) ) ) ) as distance", [$latitude, $longitude, $latitude])
            ->having("distance", "<", $request->distance)
            ->orderBy('distance', 'asc')
            ->offset(0)
            ->limit(20);

        $data = $query->get();
1 like

Please or to participate in this conversation.