PHP code example of chuckcms / laravel-addresses

1. Go to this page and download the library: Download chuckcms/laravel-addresses library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

chuckcms / laravel-addresses example snippets




namespace App\Models;

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

class Post extends Model
{
    use HasAddresses;

    // ...
} 

$post = Post::first();
$post->addAddress([
	'label' 				=> 'My address', // '100', // defaults to: null
	'housenumber_postfix' 			=> 'B1', // defaults to: null
	'postal_code'				=> '10001', // defaults to: null
	'city'					=> 'Chuck City', // defaults to: null
	'state'					=> 'New Chuck State', // defaults to: null
	'country'				=> 'Chuckland', // defaults to: null
	'latitude'				=> 51.13128, // defaults to: null
	'longitude'				=> 4.57041, // defaults to: null
	'is_primary'				=> true, // defaults to: false
	'is_billing'				=> false, // defaults to: false
	'is_shipping'				=> false, // defaults to: false
]);

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

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

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

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

$post = Post::first();

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

use Chuckcms\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', 'Chuck City')->get();
if ($post->hasAddress($addresses)) {
	//do something
}

//OR
$addresses = Address::where('state', 'New Chuck State')->pluck('id')->toArray();
if ($post->hasAddress($addresses)) {
	//do something
}

$post = Post::first();

$primaryAddress = $post->getPrimaryAddress();

$post = Post::first();

$billingAddress = $post->getBillingAddress();

$post = Post::first();

$shippingAddress = $post->getShippingAddress();

$post = Post::first();

$labels = $post->getAddressLabels();
 php artisan vendor:publish --provider="Chuckcms\Addresses\AddressesServiceProvider" 
config/addresses.php
 php artisan migrate