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.
- Install the package via Composer:
composer require neuecommerce/addresses
- Publish the migrations (optional):
php artisan vendor:publish --tag="neuecommerce-addresses-migrations"
- Publish the configuration file (optional):
php artisan vendor:publish --tag="neuecommerce-addresses-config"
- Run the migrations:
php artisan migrate
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';
}
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();
Creating new addresses is very simple.
$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,
]);
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.
// Find the address to update
$address = $customer->addresses()->whereType('shipping')->whereDefault()->first();
// Update the address
$address->update([
'display_name' => 'Home Shipping 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.
// Find the address to delete
$address = $customer->addresses()->whereType('shipping')->whereDefault()->first();
// Delete the address
$address->delete();
composer test
Thank you for your interest. Here are some of the many ways to contribute.
- Check out our contributing guide
- Look at our code of conduct
This package is open-sourced software licensed under the MIT license.