Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mstfblci committed Oct 17, 2022
0 parents commit 6e0430d
Show file tree
Hide file tree
Showing 11 changed files with 885 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 madonetr

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
173 changes: 173 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
# Laravel Addresses

An easy way to manage Turkey addresses for Eloquent models in Laravel.
Inspired by the following packages:
- [chuckcms/laravel-addresses](https://github.com/chuckcms/laravel-addresses)
- [rinvex/laravel-addresses](https://github.com/rinvex/laravel-addresses)
- [Lecturize/Laravel-Addresses](https://github.com/Lecturize/Laravel-Addresses)

## 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
<?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

```php
$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

```php
$post = Post::first();
$address = $post->getPrimaryAddress();

$post->updateAddress($address, ['label' => 'My new address']);
```

#### Delete an address from a model

```php
$post = Post::first();
$address = $post->addresses()->first();

if ($post->deleteAddress($address)) {
//do something
}
```

#### Determine if a model has any addresses

```php
$post = Post::first();

if ($post->hasAddresses()) {
//do something
}
```

#### Determine if a model has (one of) the given address(es).

```php
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.

```php
$post = Post::first();

$primaryAddress = $post->getPrimaryAddress();
```

#### Get the billing address of the model.

```php
$post = Post::first();

$billingAddress = $post->getBillingAddress();
```

#### Get the shipping address of the model.

```php
$post = Post::first();

$shippingAddress = $post->getShippingAddress();
```

#### Get the labels of all addresses of the model.

```php
$post = Post::first();

$labels = $post->getAddressLabels();
```

## License

Licensed under [MIT license](http://opensource.org/licenses/MIT).

## Author

**Written by [Mustafa Balci](https://twitter.com/mustafabalci__) in Turkey.**
38 changes: 38 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "mstfblci/laravel-addresses",
"description": "Module for storing addresses.",
"keywords": ["laravel", "module", "addresses"],
"homepage": "https://github.com/mstfblci/laravel-addresses",
"license": "MIT",
"authors": [
{
"name": "Mustafa Balcı",
"email": "madonetr@gmail.com",
"homepage": "",
"role": "Developer"
}
],
"require": {
"php" : "^7.2.5|^8.0",
"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"
},
"autoload": {
"psr-4": {
"Madonetr\\Addresses\\": "src"
}
},
"extra": {
"laravel": {
"providers": [
"Madonetr\\Addresses\\AddressesServiceProvider"
],
"aliases": {

}
}
},
"minimum-stability": "dev"
}
53 changes: 53 additions & 0 deletions config/addresses.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

return [

'models' => [

/*
* Which model to use for the Address when using the 'HasAddresses' trait.
*
*/

'address' => Madonetr\Addresses\Models\Address::class,

],

'table_names' => [

/*
* Define the table name to use when using the 'HasAddresses' trait.
*/

'addresses' => 'addresses',

],

'column_names' => [

'model_morph_name' => 'addressable',
'model_morph_key' => 'addressable_id',
'model_morph_type' => 'addressable_type',

],

'fields' => [
'addresses' => [
'label' => 'required|string|max:255',
'type' => 'nullable|string|max:140',
'name' => 'nullable|string|max:140',
'company' => 'nullable|string|max:140',
'vat_id' => 'nullable|integer',
'vat_office' => 'nullable|string|max:140',
'address' => 'nullable|string|max:140',
'postal_code' => 'nullable|string',
'city' => 'nullable|string|max:140',
'state' => 'nullable|string|max:140',
'country' => 'nullable|string|max:140',
'is_public' => 'sometimes|boolean',
'is_primary' => 'sometimes|boolean',
'is_billing' => 'sometimes|boolean',
'is_shipping' => 'sometimes|boolean',
],
],
];
59 changes: 59 additions & 0 deletions database/migrations/create_addresses_tables.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateAddressesTables extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$tableNames = config('addresses.table_names');
$morphName = config('addresses.column_names.model_morph_name');

Schema::create($tableNames['addresses'], function (Blueprint $table) use ($morphName) {
$table->increments('id');
$table->string('label');
$table->string('type')->nullable()->default(null);

$table->string('name')->nullable()->default(null);
$table->string('company')->nullable()->default(null);

$table->bigInteger('vat_id')->nullable()->default(null);
$table->string('vat_office')->nullable()->default(null);

$table->string('address')->nullable()->default(null);
$table->string('postal_code')->nullable()->default(null);
$table->string('city')->nullable()->default(null);
$table->string('state')->nullable()->default(null);
$table->string('country')->nullable()->default(null);

$table->boolean('is_public')->default(false);
$table->boolean('is_primary')->default(false);
$table->boolean('is_billing')->default(false);
$table->boolean('is_shipping')->default(false);

$table->nullableMorphs($morphName);

$table->timestamps();
$table->softDeletes();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
$tableNames = config('addresses.table_names');

Schema::drop($tableNames['addresses']);
}
}
Loading

0 comments on commit 6e0430d

Please sign in to comment.