Giter Club home page Giter Club logo

laravel-addresses's Introduction

Latest Stable Version Total Downloads License

Laravel Addresses

Simple address and contact management for Laravel 5 with automatical geocoding to add longitude and latitude. Uses the famous Countries package by Webpatser.

Important Notice

This package is a work in progress, please use with care and feel free to report any issues or ideas you may have!

We've transferred this package to a new owner and therefor updated the namespaces to Lecturize\Addresses. The config file is now config/lecturize.php.

Installation

Require the package from your composer.json file

"require": {
	"lecturize/laravel-addresses": "dev-master"
}

and run $ composer update or both in one with $ composer require lecturize/laravel-addresses.

Next register the following service providers and facades to your config/app.php file

'providers' => [
    // Illuminate Providers ...
    // App Providers ...
    Lecturize\Addresses\AddressesServiceProvider::class,
    Webpatser\Countries\CountriesServiceProvider::class,
];
'aliases' => [
	// Illuminate Facades ...
    'Countries' => Webpatser\Countries\CountriesFacade::class,
];

Configuration & Migration

$ php artisan vendor:publish --provider="Webpatser\Countries\CountriesServiceProvider"
$ php artisan vendor:publish --provider="Lecturize\Addresses\AddressesServiceProvider"

This will create a config/countries.php, a config/lecturize.php and the migration files, that you'll have to run like so:

$ php artisan countries:migration
$ php artisan migrate

Check out Webpatser\Countries readme to see how to seed their countries data to your database.

Usage

First, add our HasAddresses trait to your model.

<?php namespace App\Models;

use Lecturize\Addresses\Traits\HasAddresses;

class Post extends Model
{
    use HasAddresses;

    // ...
}
?>
Add an Address to a Model
$post = Post::find(1);
$post->addAddress([
    'street'     => '123 Example Drive',
    'city'       => 'Vienna',
    'post_code'  => '1110',
    'country'    => 'AT', // ISO-3166-2 or ISO-3166-3 country code
    'is_primary' => true, // optional flag
]);

Alternativly you could do...

$address = [
    'street'     => '123 Example Drive',
    'city'       => 'Vienna',
    'post_code'  => '1110',
    'country'    => 'AT', // ISO-3166-2 or ISO-3166-3 country code
    'is_primary' => true, // optional flag
];
$post->addAddress($address);

Available attributes are street, city, post_code, state, country, state, note (for internal use), is_primary, is_billing & is_shipping. Optionally you could also pass lng and lat, in case you deactivated the included geocoding functionality and want to add them yourself.

Check if Model has an Address
if ($post->hasAddress()) {
    // Do something
}
Get all Addresses for a Model
$addresses = $post->addresses()->get();
Get primary/billing/shipping Addresses
$address = $post->getPrimaryAddress();
$address = $post->getBillingAddress();
$address = $post->getShippingAddress();
Update an Address for a Model
$address = $post->addresses()->first(); // fetch the address

$post->updateAddress($address, $new_attributes);
Delete an Address from a Model
$address = $post->addresses()->first(); // fetch the address

$post->deleteAddress($address); // delete by passing it as argument
Delete all Addresses from a Model
$post->flushAddresses();

License

Licensed under MIT license.

Author

Handcrafted with love by Alexander Manfred Poellmann in Vienna & Rome.

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.