centrex/laravel-addresses

Manage address in laravel

v1.0.4 2024-01-03 07:09 UTC

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.

Contents

Installation

You can install the package via composer:

composer require centrex/laravel-addresses

You can run the migrations with:

php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="addresses-config"

Usage

First, add our HasAddresses trait to your model.

<?php namespace App\Models;

use Lecturize\Addresses\Traits\HasAddresses;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasAddresses;

    // ...
}
?>
Add an Address to a Model
$post = Post::find(1);
$post->addAddress([
    'street'     => '123 Example Drive',
    'city'       => 'Vienna',
    'post_code'  => '1110',
    'country'    => 'AT', // ISO-3166-2 or ISO-3166-3 country code
    'is_primary' => true, // optional flag
]);

Alternativly you could do...

$address = [
    'street'     => '123 Example Drive',
    'city'       => 'Vienna',
    'post_code'  => '1110',
    'country'    => 'AT', // ISO-3166-2 or ISO-3166-3 country code
    'is_primary' => true, // optional flag
];
$post->addAddress($address);

Available attributes are street, street_extra, city, post_code, state, country, state, notes (for internal use). You can also use custom flags like is_primary, is_billing & is_shipping. Optionally you could also pass lng and lat, in case you deactivated the included geocoding functionality and want to add them yourself.

Check if Model has Addresses
if ($post->hasAddresses()) {
    // Do something
}
Get all Addresses for a Model
$addresses = $post->addresses()->get();
Get primary/billing/shipping Addresses
$address = $post->getPrimaryAddress();
$address = $post->getBillingAddress();
$address = $post->getShippingAddress();
Update an Address for a Model
$address = $post->addresses()->first(); // fetch the address

$post->updateAddress($address, $new_attributes);
Delete an Address from a Model
$address = $post->addresses()->first(); // fetch the address

$post->deleteAddress($address); // delete by passing it as argument
Delete all Addresses from a Model
$post->flushAddresses();

Testing

🧹 Keep a modern codebase with Pint:

composer lint

✅ Run refactors using Rector

composer refacto

⚗️ Run static analysis using PHPStan:

composer test:types

✅ Run unit tests using PEST

composer test:unit

🚀 Run the entire test suite:

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.