PHP code example of chantouch / laravel-addresses

1. Go to this page and download the library: Download chantouch/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/ */

    

chantouch / laravel-addresses example snippets


 
namespace App\Models;

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

class Post extends Model
{
    use HasAddresses;

    // ...
}

$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
]);

$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);

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

$addresses = $post->addresses()->get();

$address = $post->getPrimaryAddress();
$address = $post->getBillingAddress();
$address = $post->getShippingAddress();

$address = $post->addresses()->first(); // fetch the address

$post->updateAddress($address, $new_attributes);

$address = $post->addresses()->first(); // fetch the address

$post->deleteAddress($address); // delete by passing it as argument

$post->flushAddresses();

 
namespace App\Models;

use Chantouch\Addresses\Traits\HasContacts;
use Illuminate\Database\Eloquent\Model;

class Team extends Model
{
    use HasContacts;

    // ...
}

$post = Team::find(1);
$post->addContact([
    'first_name' => 'Alex',
    'website'    => 'https://twitter.com/AMPoellmann',
    'is_primary' => true, // optional flag
]);

use Chantouch\Addresses\Models\Address;

$address = Address::find(1);
$contacts = $address->contacts;

foreach ($contacts as $contact) {
    //
}

use Chantouch\Addresses\Models\Address;

$contact = Address::find(1)
                  ->contacts()
                  ->first();

use Chantouch\Addresses\Models\Contact;

$contact = Contact::find(1);

return $contact->address->getHtml();
bash
$ php artisan vendor:publish --provider="Chantouch\Addresses\AddressesServiceProvider"
bash
$ php artisan migrate