Laravel Address is a package to manage addresses that belong to your models. You can add addresses to any eloquent model with ease.
-
Install the package via composer:
composer require bkfdev/laravel-address
-
Publish resources (migrations and config files):
php artisan vendor:publish --provider="Bkfdev\Addressable\AddressesServiceProvider"
-
Run migrations:
php artisan migrate
-
Done!
To add addresses support to your eloquent models simply use \Bkfdev\Addressable\Traits\Addressable
trait.
// 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();
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();
Refer to the Changelog for a full history of the project.
Please raise a GitHub issue.
$ composer test
Please see contributing.md for details and a todolist.
If you discover any security related issues, please email bkfdev@gmail.com instead of using the issue tracker.
MIT. Please see the license file for more information.