mstfblci / laravel-addresses
Module for storing addresses.
Requires
- php: >=7.2.5
- illuminate/auth: ^6.0|^7.0|^8.0
- illuminate/container: ^6.0|^7.0|^8.0
- illuminate/contracts: ^6.0|^7.0|^8.0
- illuminate/database: ^6.0|^7.0|^8.0
This package is auto-updated.
Last update: 2024-10-25 22:43:17 UTC
README
An easy way to manage Turkey addresses for Eloquent models in Laravel. Inspired by the following packages:
Installation
Require the package by running
composer require madonetr/laravel-addresses
Publish configuration and migration
php artisan vendor:publish --provider="Madonetr\Addresses\AddressesServiceProvider"
This command will publish a config/addresses.php
and a migration file.
You can modify the default fields and their rules by changing both of these files.
After publishing you can run the migrations
php artisan migrate
Usage
You can use the HasAddresses
trait on any model.
<?php namespace App\Models; use Madonetr\Addresses\Traits\HasAddresses; use Illuminate\Database\Eloquent\Model; class Post extends Model { use HasAddresses; // ... }
After doing this you can use the following methods.
Add an address to a model
$post = Post::first(); $post->addAddress([ 'label' => 'My address', // required 'type' => 'Individual', // defaults to: null 'name' => 'Mustafa Balci', // defaults to: null 'company' => 'Madonetr', // defaults to: null 'vat_id' => '12314123', // defaults to: null - integer 'vat_office' => 'Yalova Vergi Dairesi', // defaults to: null 'address' => 'Main Street', // defaults to: null 'postal_code' => '10001', // defaults to: null 'city' => 'Yalova', // defaults to: null 'state' => 'New Madonetr State', // defaults to: null 'country' => 'Turkey', // defaults to: null 'is_primary' => true, // defaults to: false 'is_billing' => false, // defaults to: false 'is_shipping' => false, // defaults to: false ]);
Update an existing address
$post = Post::first(); $address = $post->getPrimaryAddress(); $post->updateAddress($address, ['label' => 'My new address']);
Delete an address from a model
$post = Post::first(); $address = $post->addresses()->first(); if ($post->deleteAddress($address)) { //do something }
Determine if a model has any addresses
$post = Post::first(); if ($post->hasAddresses()) { //do something }
Determine if a model has (one of) the given address(es).
use Madonetr\Addresses\Models\Address; $post = Post::find(); $address = Address::first(); if ($post->hasAddress($address)) { //do something } //OR if ($post->hasAddress($address->id)) { //do something } //OR $addresses = Address::where('city', 'Yalova')->get(); if ($post->hasAddress($addresses)) { //do something } //OR $addresses = Address::where('state', 'New Madonetr State')->pluck('id')->toArray(); if ($post->hasAddress($addresses)) { //do something }
This will return true when one of the given addresses belongs to the model.
Getters
You can use the following methods to retrieve addresses and certain attributes.
Get the primary address of the model.
$post = Post::first(); $primaryAddress = $post->getPrimaryAddress();
Get the billing address of the model.
$post = Post::first(); $billingAddress = $post->getBillingAddress();
Get the shipping address of the model.
$post = Post::first(); $shippingAddress = $post->getShippingAddress();
Get the labels of all addresses of the model.
$post = Post::first(); $labels = $post->getAddressLabels();
License
Licensed under MIT license.
Author
Written by Mustafa Balci in Turkey.