neuecommerce/addresses

Addresses is a polymorphic Laravel package, for addressbook management. Adding addresses to any Eloquent model was never this easy.

Fund package maintenance!
neuecommerce

dev-main / 1.0.x-dev 2024-04-01 16:50 UTC

This package is auto-updated.

Last update: 2024-05-01 17:05:16 UTC


README

GitHub Tests Action Status GitHub Code Analysis Action Status Software License Latest Version on Packagist Total Downloads

Addresses is a polymorphic Laravel package, for addressbook management.

Adding addresses to any Eloquent model was never this easy and the possibilities of address organisation is also possible since you'll be able to assign a custom type like shipping, billing, etc.. to each address, which gives the possibility of having one default address per type of address. For example, have one default shipping address and one default billing address, which is very common on e-commerce applications.

Installation

  1. Install the package via Composer:
composer require neuecommerce/addresses
  1. Publish the migrations (optional):
php artisan vendor:publish --tag="neuecommerce-addresses-migrations"
  1. Publish the configuration file (optional):
php artisan vendor:publish --tag="neuecommerce-addresses-config"
  1. Run the migrations:
php artisan migrate

Implementation

In order to add addresses support to your Eloquent models, you'll need to ensure that your model implements the NeueCommerce\Addresses\Contracts\HasManyAddressesInterface interface and the NeueCommerce\Addresses\Traits\HasManyAddresses trait.

Here's an example of a model with the proper implementation:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use NeueCommerce\Addresses\Contracts\HasManyAddressesInterface;
use NeueCommerce\Addresses\Traits\HasManyAddresses;

class Customer extends Model implements HasManyAddressesInterface
{
    use HasManyAddresses;

    public $table = 'customers';
}

Usage

In this section you'll learn how to use the address package and manage the addresses of your Eloquent Models.

For simplicity, the entity we'll use will be of a Customer entity (the one we created on the previous section):

$customer = Customer::first();

Create a new Address

Creating new addresses is very simple.

Example:
$address = $customer->addresses()->create([
    'type' => 'shipping',
    'display_name' => 'Default Shipping Address',
    'first_name' => 'John',
    'last_name' => 'Done',
    'address_1' => '3327 Colby Ave',
    'city' => 'Everett',
    'country_code' => 'US',
    'state' => 'Washington',
    'postal_code' => '98201',
    'latitude' => '47.972829',
    'longitude' => '-122.20793',
    'is_default' => true,
]);

Update an existing Address

To update an address, you'll need to fetch the address you want to update and then call the update() method on that address instance while providing the updated attributes.

Example:
// Find the address to update
$address = $customer->addresses()->whereType('shipping')->whereDefault()->first();

// Update the address
$address->update([
    'display_name' => 'Home Shipping Address',
]);

Delete an Address

To delete an address, you'll need to fetch the address you want to delete and then call the delete() method on that address instance.

Example:
// Find the address to delete
$address = $customer->addresses()->whereType('shipping')->whereDefault()->first();

// Delete the address
$address->delete();

Testing

composer test

Contributing

Thank you for your interest. Here are some of the many ways to contribute.

License

This package is open-sourced software licensed under the MIT license.