Basic Usage Countries Scopes
Scopes in Laravel allow you to reuse query logic efficiently.
The whereSlug()
method shown here simplifies filtering countries by their slug.
This scope is especially useful when you need to filter and find countries based on their slugs.
It keeps your queries cleaner and reusable across your project.
Where Name
The name
field allows you to search for a country in any translation, ensuring the correct country is always found regardless of the language used.
use Lwwcas\LaravelCountries\Models\Country;
Country::whereName('United Kingdom')->first();
Where Slug
The slug
field is a URL-friendly version of the country name, typically generated by converting the country name into lowercase, replacing spaces with hyphens, and removing special characters.
This allows for easier search and filtering in URLs.
use Lwwcas\LaravelCountries\Models\Country;
Country::whereSlug('spain')->first();
Where Oficial Name
The official_name
field contains the formal or official name of the country as recognized internationally. It is typically used in official documents and formal contexts.
use Lwwcas\LaravelCountries\Models\Country;
Country::whereOficialName('Republic of Bulgaria')->first();
Where Uid
The uid
field is a unique identifier (ULID) assigned to each country. It ensures that every country record has a globally unique and secure identifier that is not easily predictable, making it ideal for distributed systems and ensuring data integrity across various systems.
use Lwwcas\LaravelCountries\Models\Country;
Country::whereUid('01J95Z30PMTMAQC8X1S4Y5SRYY')->first();