Giter Club home page Giter Club logo

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.

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

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.