PHP code example of ihangan / laravel-moldova-cuatm
1. Go to this page and download the library: Download ihangan/laravel-moldova-cuatm 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/ */
ihangan / laravel-moldova-cuatm example snippets
use Ihangan\MoldovaCuatm\Models\Location;
use Ihangan\MoldovaCuatm\Enums\LocationType;
// By the official CUATM code or by slug.
Location::whereCode('0111001')->first();
Location::where('slug', 'chisinau')->first();
// Every raion.
Location::ofType(LocationType::Raion)->get();
// Top-level units only.
Location::roots()->get();
use Spatie\Translatable\Facades\Translatable;
Translatable::fallback(fallbackLocale: 'ro', fallbackAny: true);
$village = Location::where('slug', 'dobrogea')->first();
$village->parent; // the town it belongs to
$village->ancestors(); // [town, sector, municipality], nearest first
$sector = Location::where('slug', 'botanica')->first();
$sector->children; // towns and villages under Botanica
use Ihangan\MoldovaCuatm\Facades\Cuatm;
Cuatm::findByCode('0111001');
Cuatm::findBySlug('chisinau');
Cuatm::roots(); // raioane, municipalities, Gagauzia, Transnistria
Cuatm::raioane();
Cuatm::childrenOf($raion);
Cuatm::tree(); // roots with their children eager-loaded
use Ihangan\MoldovaCuatm\Facades\Cuatm;
use Livewire\Component;
class LocationPicker extends Component
{
/** @var array<int, int> the selected location id at each level */
public array $path = [];
public function selectLevel(int $level, ?int $id): void
{
$this->path = array_slice($this->path, 0, $level); // drop the deeper levels
if ($id !== null) {
$this->path[$level] = $id;
}
}
public function render()
{
$levels = collect([Cuatm::roots()]);
foreach ($this->path as $id) {
$children = Cuatm::childrenOf($id);
if ($children->isEmpty()) {
break; // reached the bottom of the tree
}
$levels->push($children);
}
return view('livewire.location-picker', ['levels' => $levels]);
}
}