Support the ongoing development of Laravel.io →
Database Eloquent

HI, Ive got a location query that Im running and he having is not matching, on mysql or Sqlite , Now sqlite dose not have some maths function so Ive added the required maths functions to sqlite .

this is my query

        return $this->createModel()->select(
            DB::raw("`name`, `latitude` , `longitude` ,  ( 6371 * acos( cos( radians(?) ) * cos( radians( `latitude` ) ) * cos( radians( `longitude` ) - radians(?) ) + sin( radians(?) ) * sin( radians( `latitude` ) ) )) AS `distance`"))
            ->groupBy('name')
            ->havingRaw("distance < ?")
            ->orderBy("distance")
            ->setBindings([$latitude, $longitude, $latitude, $radius])
            ->get();

and this is in my app service provider adding the required maths functions to sqlite.

		$pdo = DB::connection('sqlite')->getPdo();
		$pdo->sqliteCreateFunction('cos', 'cos', 1);
		$pdo->sqliteCreateFunction('acos', 'acos', 1);
		$pdo->sqliteCreateFunction('sin', 'sin', 1);
		$pdo->sqliteCreateFunction('radians', 'deg2rad', 1);
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

apps4u apps4u Joined 7 Apr 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.