bkfdev/laravel-address

v2.0.5 2023-09-11 12:35 UTC

This package is auto-updated.

Last update: 2024-11-11 15:03:38 UTC


README

Latest Version on Packagist Total Downloads Build Status StyleCI

Laravel Address is a package to manage addresses that belong to your models. You can add addresses to any eloquent model with ease.

Installation

  1. Install the package via composer:

    composer require bkfdev/laravel-address
  2. Publish resources (migrations and config files):

    php artisan vendor:publish --provider="Bkfdev\Addressable\AddressesServiceProvider"
  3. Run migrations:

    php artisan migrate
  4. Done!

Usage

To add addresses support to your eloquent models simply use \Bkfdev\Addressable\Traits\Addressable trait.

Manage your addresses

// Get instance of your model
$user = new \App\Models\User::find(1);

// Create a new address
$user->addresses()->create([
    'label' => 'Default Address',
    'country_code' => 'dz',
    'street' => '56 john doe st.',
    'state' => 'Canterbury',
    'city' => 'Christchurch',
    'postal_code' => '7614',
    'latitude' => '31.2467601',
    'longitude' => '29.9020376',
    'is_primary' => true,
]);

// Create multiple new addresses
$user->addresses()->createMany([
    [...],
    [...],
    [...],
]);

// Find an existing address
$address = Bkfdev\Addressable\Models\Address::find(1);

// Update an existing address
$address->update([
    'label' => 'Default Work Address',
]);

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

// Alternative way of address deletion
$user->addresses()->where('id', 123)->first()->delete();

Manage your addressable model

The API is intuitive and very straight forward, so let's give it a quick look:

// Get instance of your model
$user = new \App\Models\User::find(1);

// Get attached addresses collection
$user->addresses;

// Get attached addresses query builder
$user->addresses();

// Scope Primary Addresses
$primaryAddresses = Bkfdev\Addressable\Models\Address::isPrimary()->get();

// Scope Addresses in the given country
$algerianAddresses = Bkfdev\Addressable\Models\Address::inCountry('dz')->get();

Changelog

Refer to the Changelog for a full history of the project.

Support

Please raise a GitHub issue.

Testing

$ composer test

Contributing

Please see contributing.md for details and a todolist.

Security

If you discover any security related issues, please email bkfdev@gmail.com instead of using the issue tracker.

Credits

License

MIT. Please see the license file for more information.