customd / laravel-addressable
Laravel Addressable is a Laravel package to manage addresses that belong to your models. You can add addresses to any eloquent model with ease.
Requires
- php: ^8.0.0
- illuminate/console: ^9.0|^10.0
- illuminate/database: ^9.0|^10.0
- illuminate/support: ^9.0|^10.0
- jackpopp/geodistance: ^1.2.0
- rinvex/countries: ^8.0.0
- spatie/laravel-package-tools: ^1.9.2
Requires (Dev)
- illuminate/container: ^9.0|^10.0
- nunomaduro/collision: ^6.0|^7.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5.0|^10.0
README
Custom D Addressable is a Laravel package to manage addresses that belong to your models. You can add addresses to any eloquent model with ease.
Installation
-
Install the package via composer:
composer require customd/laravel-addresses
-
Publish resources (migrations and config files):
php artisan vendor:publish --provider="CustomD\Addressable\AddressesServiceProvider"
-
Run migrations:
php artisan migrate
-
Done!
Usage
To add addresses support to your eloquent models simply use \CustomD\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', 'given_name' => 'Jane', 'family_name' => 'Doe', 'organization' => 'Custom D', 'country_code' => 'eg', '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 = app('addressable.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 = app('addressable.address')->isPrimary()->get(); // Scope Addresses in the given country $egyptianAddresses = app('addressable.address')->inCountry('eg')->get(); // Find all users within 5 kilometers radius from the latitude/longitude 31.2467601/29.9020376 $fiveKmAddresses = \App\Models\User::findByDistance(5, 'kilometers', '31.2467601', '29.9020376')->get(); // Alternative method to find users within certain radius $user = new \App\Models\User(); $users = $user->lat('31.2467601')->lng('29.9020376')->within(5, 'kilometers')->get();
Changelog
Refer to the Changelog for a full history of the project.
Support
Please raise a GitHub issue.
Contributing & Protocols
Thank you for considering contributing to this project! The contribution guide can be found in CONTRIBUTING.md.
Bug reports, feature requests, and pull requests are very welcome.
Security Vulnerabilities
If you discover a security vulnerability within this project, please send an e-mail to help@customd.com. All security vulnerabilities will be promptly addressed.
License
This software is released under The MIT License (MIT).