codelyftlab's avatar

Searching within 10KM radus.

Good evening, i have some problem with search feature.

PropertySearchController
public function __invoke(Request $request)
    {
        return Property::with('city')
			//.....
            ->when($request->geoobject, function($query) use ($request) {
                $geoobject = Geoobject::find($request->geoobject);
                if ($geoobject) {
                    $condition = "(
                        6371 * acos(
                            cos(radians(" . $geoobject->lat . "))
                            * cos(radians(`lat`))
                            * cos(radians(`long`) - radians(" . $geoobject->long . "))
                            + sin(radians(" . $geoobject->lat . ")) * sin(radians(`lat`))
                        ) < 10
                    )";
                    $query->whereRaw($condition);
                }
            })
            ->get();
    }

And here is my test code

    public function test_property_search_by_geoobject_returns_correct_results(): void
    {
        $owner = User::factory()->create(['role_id' => Role::ROLE_OWNER]);
        $cityId = City::value('id');
        $geoobject = Geoobject::first();

        $propertyNear = Property::factory()->create([
            'owner_id' => $owner->id,
            'city_id' => $cityId,
            'lat' => $geoobject->lat,
            'long' => $geoobject->long,
        ]);

        $propertyFar = Property::factory()->create([
            'owner_id' => $owner->id,
            'city_id' => $cityId,
            'lat' => $geoobject->lat + 10,
            'long' => $geoobject->long - 10,
        ]);

        $response = $this->getJson('/api/search?geoobject=' . $geoobject->id);

        $response->assertStatus(200);
        $response->assertJsonCount(1);
        $response->assertJsonFragment(['id' => $propertyNear->id]);
    }

And here is my error

SQLSTATE[HY000]: General error: 1 no such function: acos (Connection: sqlite, SQL: select * from "properties" where (
                        6371 * acos(
                            cos(radians(40.689247))
                            * cos(radians(`lat`))
                            * cos(radians(`long`) - radians(-74.044502))
                            + sin(radians(40.689247)) * sin(radians(`lat`))
                        ) < 10
                    ))

Im using laravel v 10.x with php 8.1.

0 likes
0 replies

Please or to participate in this conversation.