Skip to content

01. How to use

Luca Longo edited this page Aug 10, 2020 · 1 revision

Installation

You can install the package via composer:

composer require masterix21/laravel-addressable

Next, you must publish the migrations files and the config

php artisan vendor:publish --provider "Masterix21\Addressable\AddressableServiceProvider"

How to use with your models

The package has three built-in traits: HasAddresses, HasBillingAddresses and HasShippingAddresses.

HasAddresses

It's the standard trait that will implement addresses with indistinct functionality.

use Masterix21\Addressable\Models\Concerns\HasAddresses;

class User {
    use HasAddresses;
}

// Retrieve all user's addresses
$user = User::with(['addresses'])->first();

HasBillingAddresses

It will implement the supports for the billing addresses.

use Masterix21\Addressable\Models\Concerns\HasBillingAddresses;

class User {
    use HasBillingAddresses;
}

// Retrieve all user's billing addresses
$user = User::with(['billingAddresses'])->first();

// or the billing address marked as primary
$user = User::with(['billingAddress'])->first();

HasShippingAddresses

It will implement the supports for the shipping addresses.

use Masterix21\Addressable\Models\Concerns\HasShippingAddresses;

class User {
    use HasShippingAddresses;
}

// Retrieve all user's shipping addresses
$user = User::with(['shippingAddresses'])->first();

// or the shipping address marked as primary
$user = User::with(['shippingAddress'])->first();