简体   繁体   中英

How to get distance from latitude and longitude from mysql Address table which have latitude and longitude

 $distance = DB::select(
                DB::raw("
                SELECT a.distance
                FROM ( SELECT user_id, street, store_name,
                    (
                        '".$mean_radius_of_Earth."' * 
                        acos(cos(radians('".$latitude."')) * 
                        cos(radians(latitude)) * 
                        cos(radians(longitude) - 
                        radians('".$longitude."')) + 
                        sin(radians('".$latitude."')) * 
                        sin(radians(latitude)))
                    ) AS distance
                    FROM addresses ) AS a
                    WHERE id= '". $address_id."' 

                ")
            );

I have to get all the column of the particular row using this address_id which is the primary key but i am getting the error:- Column not found: 1054 Unknown column 'id' in 'where clause' but when i am trying to achieve that with user_id then i am getting that all fields data ,user_id(which is not primary_key) your help will be much appreciated, thanks in Advance and sorry for the improper english

You don't select id in your subrequest so or you make the select in the subquery or you select the field id in your subselect:

$distance = DB::select(
            DB::raw("
            SELECT a.distance
            FROM ( SELECT user_id, street, store_name,
                (
                    '".$mean_radius_of_Earth."' * 
                    acos(cos(radians('".$latitude."')) * 
                    cos(radians(latitude)) * 
                    cos(radians(longitude) - 
                    radians('".$longitude."')) + 
                    sin(radians('".$latitude."')) * 
                    sin(radians(latitude)))
                ) AS distance
                FROM addresses 
                WHERE id= '". $address_id."') AS a                    

            ")
        );

In situation like this I would leverage the power of Eloquent's Appends functionality.

In your situation it might look a little like the following,

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    protected $appends = ['distance'];

    /**
     * Get the distance for the user.
     *
     * @return int
     */
    public function getDistanceAttribute()
    {
        $distance = // Crazy math for distance insert here

        // To get address long and lat stored in separate database 
        // I would eager load them in the query call and then in this 
        // function you'd already have the values ready to go in memory
        $distance = $this->address->long...

        return $distance;
    }
}

The beauty of this solution is you can leverage Eloquent's querying, prevent SQL injection messes, and keep things tied to your Model not the query.

use App\User;

foreach(User::all() as $user) {
    echo $user->distance; // Print's out the distance you wanted
}

echo User::where('id', '=', $address_id)->first()->distance; // Prints out specific user's distance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM