Giter Site home page Giter Site logo

divineomega / laravel-addresses Goto Github PK

View Code? Open in Web Editor NEW
9.0 5.0 4.0 29 KB

Laravel Addresses

License: GNU Lesser General Public License v3.0

PHP 100.00%
addresses laravel-5-package geocoding distance-calculation address-validation laravel-package

laravel-addresses's Introduction

Laravel Addresses

Laravel Addresses is a package that lets you associate addresses with your Laravel Eloquent models.

Features:

  • Automatic geocoding of addresses on change, provided by the Google Maps API
  • Validation of address details (country, postcode)
  • Conversion of ISO country code to country name
  • Ability to store meta data about addresses - e.g. ['type' => 'delivery', 'name' => 'home_address']

Installation

To install Laravel Addresses, just run the following Composer command.

composer require divineomega/laravel-addresses

Configuration

Run the following Artisan command to publish the configuration file.

php artisan vendor:publish --provider="DivineOmega\LaravelAddresses\ServiceProvider" --force

This will create the default configuration file at config/addresses.php.

Note that by default, you require a Google Maps API key in order to provide address geocoding and distance calculations. If you do not wish to use geocoding, this can be disabled in the configuration.

Strict geocoding

By default, geocoding is configured as "lenient"; if, for example, the name of a real city is given but the postcode and street address refer to a nonexistent place, it will geocode as the center of that city.

Set the geocoding.strict flag to true in the configuration file to instead fail to geocode in this scenario.

Usage

Assign the HasAddresses trait to the model you wish to have associated addresses. For example, you could give the default User model address, as shown below.

<?php

namespace App;

use DivineOmega\LaravelAddresses\Traits\HasAddresses;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable, HasAddresses;

    /* ... */
}

Retrieve addresses

$addresses = $user->addresses()->get();

Create new address

$user->addresses()->create([
    'line_1' => '10 Downing Street',
//  'line_2' => '',
    'town_city' => 'Westminster',
    'state_county' => 'London',
    'postcode' => 'SW1A 2AA',
    'country_code' => 'GBR',
]);

Geocoding

Geocoding is automatic when an address is created or updated. You can check if an address was successfully geocoding using the isGeocoded method.

$address = $user->addresses()->first();

if ($address->isGeocoded()) {
    dd([$address->latitude, $address->longitude]);
}

You can also manually geocode the address if needed.

$address = $user->addresses()->first();
$address->geocode();
$address->save();

Note that geocoding can fail, in which case, you can detect that it failed by checking whether the address is geocoded after attempting geocoding:

$address->geocode();

if (!$address->isGeocoded()) {
    // Handle geocoding failure here.
}

If there was an existing latitude/longitude set and geocoding fails, these are cleared.

$address->geocode(); // Succeeds

// Change the address details here.

$address->geocode(); // Fails

// Latitude and longitude are now null.

Validation

Validation is automatic when an address is created or updated. You can expect an appropriate exception to be thrown if validation fails.

  • InvalidCountryException - Provided country_code is not a valid ISO 3166-1 alpha-3 country code.
  • InvalidUKPostcodeException - If the address is within the UK, the provided postcode is not a valid UK postcode.

You can also manually validate the address if needed.

$address = $user->addresses()->first();

try {
    $address->validate();
} catch (\DivineOmega\LaravelAddresses\Exceptions\InvalidUKPostcodeException $e) {
    return back()->withErrors(['Invalid UK postcode.']);
}

Distance calculation

The distance between two different addresses can be calculated using the distanceTo method.

$address1 = $user->addresses[0];
$address2 = $user->addresses[1];

$distanceKilometres = $address1->distanceTo($address2);

By default the direct distance is calculated (as the crow flies). If you want, you can specify a different type of distance calculation, such as driving distance.

use \DivineOmega\LaravelAddresses\DistanceStrategies\Driving;
use \DivineOmega\LaravelAddresses\DistanceStrategies\Walking;
use \DivineOmega\LaravelAddresses\DistanceStrategies\Cycling;

$drivingDistanceKm = $address1->distanceTo($address2, Driving::class);
$walkingDistanceKm = $address1->distanceTo($address2, Walking::class);
$cyclingDistanceKm = $address1->distanceTo($address2, Cycling::class);

laravel-addresses's People

Contributors

divineomega avatar jameswilddev avatar stejaysulli avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

laravel-addresses's Issues

Feature Request: One-to-one relationship trait

At the moment we have a hasAddresses trait, which is great for implementations that require a number of addresses to be stored against a model, but we could also do with a matching hasAddress trait for one-to-one relationships.

Dependencies not compatible with Laravel 7

    - divineomega/laravel-addresses v1.2.0 requires langleyfoxall/simple-google-maps ^1.0 -> satisfiable by langleyfoxall/simple-google-maps[v1.0.0].
    - Installation request for divineomega/laravel-addresses ^1.2 -> satisfiable by divineomega/laravel-addresses[v1.2.0].
    - Conclusion: remove laravel/framework v7.15.0
    - Conclusion: don't install laravel/framework v7.15.0
    - langleyfoxall/simple-google-maps v1.0.0 requires laravel/framework ^5.1||^6.0 -> satisfiable by laravel/framework[5.1.x-dev, 5.2.x-dev, 5.3.x-dev, 5.4.x-dev, 5.5.x-dev, 5.6.x-dev, 5.7.x-dev, 5.8.x-dev, 6.x-dev].
    - Can only install one of: laravel/framework[5.4.x-dev, v7.15.0].
    - Can only install one of: laravel/framework[5.5.x-dev, v7.15.0].
    - Can only install one of: laravel/framework[5.6.x-dev, v7.15.0].
    - Can only install one of: laravel/framework[5.7.x-dev, v7.15.0].
    - Can only install one of: laravel/framework[5.8.x-dev, v7.15.0].
    - Can only install one of: laravel/framework[6.x-dev, v7.15.0].
    - Can only install one of: laravel/framework[5.1.x-dev, v7.15.0].
    - Can only install one of: laravel/framework[5.2.x-dev, v7.15.0].
    - Can only install one of: laravel/framework[5.3.x-dev, v7.15.0].
    - Installation request for laravel/framework (locked at v7.15.0, required as ^7.0) -> satisfiable by laravel/framework[v7.15.0].

I get this while attempting to install on Laravel 7 -- It looks like this is an issue in langleyfoxall/simple-google-maps rather than this package, but in case this is of use to you I have included it here as well

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.