craigzearfoss / addressable-trait
Trait to add addressable functionality to a Laravel 5.1 eloquent model.
Requires
- php: >=5.5.9
- illuminate/database: 5.1.*|5.2.*|5.3
This package is not auto-updated.
Last update: 2024-11-09 19:56:26 UTC
README
This adds methods to format addresses and phone numbers to an Eloquent model in Laravel 5. It also adds geocoding functionality using GeocoderLaravel.
The AddressableTrait file is pretty simple and easy to understand so modify it to suit your needs. If you have additional functionality that you would like to see added please let me know.
Composer Install
It can be found on Packagist. The recommended way is through composer.
Edit composer.json
and add:
{ "require": { "craigzearfoss/addressable-model": "dev-master" } }
And install dependencies:
$ composer update
If you do not have Composer installed, run these two commands:
$ curl -sS https://getcomposer.org/installer | php
$ php composer.phar install
Configuration
In your model add the AddressableTrait.
<?php // ... use Craigzearfoss\AddressableModel\AddressableTrait; class MyModel extends Model { use AddressableTrait;
Usage
The AddressableTrait assumes that your model has the following fields. You are not required to have all of the fields, only the fields that that are needed for the methods you want to use.
firstname
(string) - used byfullname
andreversename
methodslastname
(string) - used byfullname
andreversename
methodsaddress
(string)address2
(string)city
(string)state_id
(integer) - references the fieldabbrev
in astates
tablepostcode
(string)country_id
(integer) - references the fieldabbrev
in acountries
tablelat
(float) nullable - stores the latitude retrieved from GeocoderLaravellng
(float) nullable - stores the longitude retrieved from GeocoderLaravelphone
(string)fax
(string)
Formatting Methods:
fullname
reversename
formattedAddress
formattedPhone
formattedFax
Geocoder Methods
fetchGeocode
- returns the geocode array for the current recordfetchCoordinates
- returns an array with the latitude and longitude for the current recordupdateGeocode
- updates thelat
andlng
fields for the current modellookupGeocode
- makes a call to Google maps for the specified address and return the geocode arrayloopupCoordinates
- makes a call to Google maps for the specified address and returns an array with the latitude and longitudedistance
- calculates the distance between the current record and the specified latitude and longitudescopeDistance
- adds a location coordinate and distance to a query
Changelog
Support
Please open an issue on GitHub
Contributor Code of Conduct
Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.
License
AddressableModel is released under the MIT License. See the bundled LICENSE file for details.