PHP code example of maxiewright / laravel-tt-addresses
1. Go to this page and download the library: Download maxiewright/laravel-tt-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/ */
maxiewright / laravel-tt-addresses example snippets
use MaxieWright\TrinidadAndTobagoAddresses\Database\Seeders\DivisionSeeder;
use MaxieWright\TrinidadAndTobagoAddresses\Database\Seeders\CitySeeder;
public function run(): void
{
$this->call([
DivisionSeeder::class,
CitySeeder::class,
// ... your other seeders
]);
}
return [
// Customise table names if they conflict with existing tables
'tables' => [
'divisions' => 'tt_divisions',
'cities' => 'tt_cities',
],
// ISO country code
'country_code' => 'TT',
];
use MaxieWright\TrinidadAndTobagoAddresses\Models\Division;
use MaxieWright\TrinidadAndTobagoAddresses\Models\City;
use MaxieWright\TrinidadAndTobagoAddresses\Enums\DivisionType;
// Get all divisions
$divisions = Division::all();
// Get only Trinidad divisions
$trinidadDivisions = Division::trinidad()->get();
// Get only Tobago
$tobago = Division::tobago()->first();
// Get divisions by type
$boroughs = Division::ofType(DivisionType::Borough)->get();
$regionalCorporations = Division::ofType(DivisionType::RegionalCorporation)->get();
// Get all cities in a division
$chaguanasCities = Division::where('abbreviation', 'CHA')
->first()
->cities;
// Find a city
$portOfSpain = City::where('name', 'Port-of-Spain')->first();
$portOfSpain->division->name; // "Port of Spain"
$portOfSpain->island; // "Trinidad"
// Get full location string
$city = City::where('name', 'Scarborough')->first();
$city->full_location; // "Scarborough, Tobago"
use MaxieWright\TrinidadAndTobagoAddresses\Enums\DivisionType;
$type = DivisionType::RegionalCorporation;
$type->label(); // "Regional Corporation"
$type->island(); // "Trinidad"
// Filament support
$type->getLabel(); // "Regional Corporation"
use Illuminate\Database\Eloquent\Model;
use MaxieWright\TrinidadAndTobagoAddresses\Concerns\HasTrinidadAndTobagoAddress;
class Customer extends Model
{
use HasTrinidadAndTobagoAddress;
protected $fillable = [
'name',
'address_line_1',
'address_line_2',
'division_id',
'city_id',
];
}
use Filament\Forms\Components\Select;
use MaxieWright\TrinidadAndTobagoAddresses\Models\Division;
use MaxieWright\TrinidadAndTobagoAddresses\Models\City;
// Division select
Select::make('division_id')
->label('Division')
->options(Division::pluck('name', 'id'))
->searchable()
->preload()
->live(),
// City select (filtered by division)
Select::make('city_id')
->label('City/Town/Village')
->options(function (callable $get) {
$divisionId = $get('division_id');
if (!$divisionId) {
return City::pluck('name', 'id');
}
return City::where('division_id', $divisionId)
->pluck('name', 'id');
})
->searchable()
->preload(),
use Filament\Tables\Filters\SelectFilter;
use MaxieWright\TrinidadAndTobagoAddresses\Enums\DivisionType;
SelectFilter::make('type')
->options(DivisionType::class),