fairdigital / laravel-addresses
Simple address and contact management for Laravel 5.
This package's canonical repository appears to be gone and the package has been frozen as a result.
Installs: 1 622
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 4
Forks: 30
Open Issues: 0
Requires
- php: >=5.6.4|^7.0
- illuminate/database: >=5.3
- illuminate/support: >=5.3
- rinvex/country: ^3.1
Requires (Dev)
- doctrine/dbal: 2.5.x
- laravel/framework: >=5.3
- mockery/mockery: 0.9.*
- phpunit/phpunit: ^6.0
README
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!
Installation
Require the package from your composer.json
file
"require": { "fairdigital/laravel-addresses": "dev-master" }
and run $ composer update
or both in one with $ composer require fairdigital/laravel-addresses
.
Next register the following service providers to your config/app.php
file if Laravel<5.5
'providers' => [ // Illuminate Providers ... // App Providers ... FairDigital\Addresses\AddressesServiceProvider::class, ];
Configuration & Migration
$ php artisan vendor:publish --provider="FairDigital\Addresses\AddressesServiceProvider"
This will create a config/addresses.php
and the migration files, that you'll have to run like so:
$ php artisan countries:migration $ php artisan migrate
Usage
First, add our HasAddresses
trait to your model.
<?php namespace App\Models; use FairDigital\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 FairDigital in Melbourne, Australia.