Search code examples
phpmysqllaravellumen

Laravel Raw Query giving error in join with raw statement


I have two tables users and db_ips and I need to compare users lattitude and longitude with two three decimal points value with db_ips table column lat lon, For this I write query in laravel.

Query

$users = \DB::table('users')
         ->select("id", "latitude", "longitude")
         ->Join('IPAddress AS IP', \DB::raw('ROUND(IP.lat,3)'), '=', \DB::raw('ROUND(users.latitude,3)'))
         ->where('users.login_tries', "=", 0)
         ->where('users.latitude', "<>", "")
         ->whereNotNull('users.latitude')
         ->orderBy("id", "ASC")->limit(100)->get();
return !$users->isEmpty() ? $users : [];

For two three decimal comparison I use ROUND medthod in join but when I run the query I get following error instead of results

Error

Too few arguments to function Illuminate\Database\Query\Builder::join(), 1 passed in /home/rizwan/php/fayvo/archive/app/Console/Commands/SyncUserLocat
ion.php on line 139 and at least 2 expected

I also try to raw query but unknow column_name in field_list error received


Solution

  • join(string $table, string $first, string|null $operator = null, string|null $second = null, string $type = 'inner', bool $where = false)

    According to join API, it need at least 2 parameters, so change your code like this:

    ->Join('IPAddress AS IP', \DB::raw('ROUND(IP.lat,3)'), '=', \DB::raw('ROUND(users.latitude,3)'))