{"id":13608122,"url":"https://github.com/malhal/Laravel-Geographical","last_synced_at":"2025-04-12T14:32:05.152Z","repository":{"id":11460945,"uuid":"69748019","full_name":"malhal/Laravel-Geographical","owner":"malhal","description":"Easily add longitude and latitude columns to your records and use inherited functionality for calculating distances","archived":false,"fork":false,"pushed_at":"2025-02-25T19:22:16.000Z","size":38,"stargazers_count":284,"open_issues_count":9,"forks_count":53,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-03-26T11:16:40.059Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/malhal.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-10-01T15:58:27.000Z","updated_at":"2025-02-25T19:22:20.000Z","dependencies_parsed_at":"2022-09-13T05:13:17.790Z","dependency_job_id":"41bb38fd-5fdc-4506-aaf3-b23ff3b0a91c","html_url":"https://github.com/malhal/Laravel-Geographical","commit_stats":{"total_commits":25,"total_committers":12,"mean_commits":"2.0833333333333335","dds":0.6799999999999999,"last_synced_commit":"cd8252c966a62acd52a6ca4f16235b1530a07478"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/malhal%2FLaravel-Geographical","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/malhal%2FLaravel-Geographical/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/malhal%2FLaravel-Geographical/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/malhal%2FLaravel-Geographical/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/malhal","download_url":"https://codeload.github.com/malhal/Laravel-Geographical/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248581265,"owners_count":21128137,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T19:01:24.465Z","updated_at":"2025-04-12T14:32:04.919Z","avatar_url":"https://github.com/malhal.png","language":"PHP","funding_links":[],"categories":["PHP"],"sub_categories":[],"readme":"# Laravel Geographical\nEasily add longitude and latitude columns to your records and use inherited functionality for calculating distances.\n\nFirst either update your database or add this to a migration for each model:\n\n```php\n$table-\u003edouble('longitude');\n$table-\u003edouble('latitude');\n```\n\nFinally, edit you model to use the Geographical Trait, as the example below:\n```php\n\u003c?php\n\nnamespace App\\Models;\n\nuse Malhal\\Geographical\\Geographical;\nuse Illuminate\\Database\\Eloquent\\Model;\n\nclass ModelExample extends Model\n{\n    use Geographical;\n```\n\n### 1. Distance\n\nFind the distance to all the entries in your table from a particular location.\n\n```php\n$query = ModelExample::distance($latitude, $longitude);\n$asc = $query-\u003eorderBy('distance', 'ASC')-\u003eget();\n ```\n\n### 2. Geofence\n\nFind all the entries in your table inside a circular geo-fence.\n\n```php\n$query = ModelExample::geofence($latitude, $longitude, $inner_radius, $outer_radius);\n$all = $query-\u003eget();\n```\n\n\u003e Use `$inner_radius`= 0 \u0026 `$outer_radius` = any number in miles that you desire.\n\n### Units\n\nThe default unit of distance is **miles**. You can change it to **kilometers** by putting this in your model\n```php\nprotected static $kilometers = true;\n```\n\n### Notes\n\n1. The method returns a `Eloquent\\Builder` object so that you can add optional conditions if you want.\n2. If you require to select only a certain columns, it can be achieved by using `select()`.\n    ```php\n    ModelExample::select('id', 'name')-\u003edistance($latitude, $longitude);\n    ```\n    (`select()` should precede the `distance()/geofence()`)\n3. You can use `distance` as an aggregate column in the result.\n(Aggregate columns cannot be used in `WHERE`, use `HAVING` to execute any condition.)\n4. If you use different column names for latitude and longitude, mention them in the Model.php\n    ```php\n    const LATITUDE  = 'lat';\n    const LONGITUDE = 'lng';\n    ```\n\n### Options\n\n1. You may pass an array of options as the third parameter of the distance method, these options will allow you to set a new table name or column names at runtime.\n     ```php\n    $query = Model::distance($latitude, $longitude, $options);\n    ```\n\n2. There are three fields you set with the options parameter at runtime: table, latitude_column, and longitude_column:\n    ```php\n    $options = [\n       'table' =\u003e 'coordinates',\n       'latitude_column' =\u003e 'lat',\n       'longitude_column' =\u003e 'lon'\n    ]\n   \n    Model::select('id', 'name')-\u003edistance($latitude, $longitude, $options);\n    ```\n3. The table field will allow you to set the table from which the the coordinates will be selected at runtime, allowing you to join the coordinates to your model from another table.\n    ```php\n   \n   Model::join('locations', function($join){ \n               $join-\u003eon('model.id', '=', 'locations.model_id');\n           })\n           -\u003eselect('id', 'name')\n           -\u003edistance($latitude, $longitude, ['table' =\u003e 'locations']);\n    ```\n4. The latitude_column and longitude_column fields can be used to set the column names for a joined table, or to override the default column names (including those set on your model) at runtime. If you don't set the column name fields at runtime when using a joined table then column names set on your model, or the defaults of 'latitude' and 'longitude' will be used. Setting `const LATITUDE  = 'lat'` or `const LONGITUDE = 'lng'` on a joined model will have no effect.\n                                                                                                                                                                                                     \n \n## Installation\n\n[PHP](https://php.net) 5.6.4+ and [Laravel](http://laravel.com) 5+ are required.\n\nTo get the latest version of Laravel Geographical, simply require the project using [Composer](https://getcomposer.org):\n\n```bash\n$ composer require malhal/laravel-geographical\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmalhal%2FLaravel-Geographical","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmalhal%2FLaravel-Geographical","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmalhal%2FLaravel-Geographical/lists"}