PHP code example of apriil / postal-codes

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

    

apriil / postal-codes example snippets


use Apriil\PostalCodes\PostalCode;

$postal = PostalCode::find('0001');

$postal->id;       // "0001"
$postal->name;     // "Oslo"
$postal->category; // Category::Mailbox

use Apriil\PostalCodes\Enums\Category;
use Apriil\PostalCodes\PostalCode;

// Street-address codes only
$streetCodes = PostalCode::where('category', Category::Address)->get();

// PO box codes in Oslo
$osloPostboxes = PostalCode::where('name', 'Oslo')
    ->where('category', Category::Mailbox)
    ->get();

// Compare on the model
$postal = PostalCode::find('0050');
$postal->category === Category::Address; // true

$isValid = PostalCode::where('id', '7030')->exists();

$osloCodes = PostalCode::where('name', 'Oslo')->get();

use Apriil\PostalCodes\PostalCode;
use Illuminate\Validation\Rule;

$request->validate([
    'postal_code' => [
        '

$postalCodes = PostalCode::query()
    ->orderBy('name')
    ->orderBy('id')
    ->get(['id', 'name', 'category']);

use Apriil\PostalCodes\PostalCode;

class User extends Model
{
    public function postalCode()
    {
        return $this->belongsTo(PostalCode::class, 'postal_code', 'id');
    }
}