PHP code example of laravingo / turkiye-validator

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

    

laravingo / turkiye-validator example snippets


return [
    // Phone format options: 'E164' (+905...), 'NATIONAL' (05...), 'RAW' (5...)
    'phone_format' => 'E164', 

    // Character used for masking identity numbers
    'mask_char' => '*', 
];

$request->validate([
    'identity_number' => 'ne',
    'tax_number'      => 'nullable|tax_id',
    'plate_code'      => '      => '

use Laravingo\TurkiyeValidator\Facades\Turkiye;

// Get All Cities (Plate Code => Name)
$cities = Turkiye::cities(); 
// Returns: [1 => 'Adana', ..., 34 => 'İstanbul', ...]

// Get Districts for a City (by Plate Code)
$districts = Turkiye::districts(34); 
// Returns: ['Adalar', 'Arnavutköy', 'Ataşehir', ...]

// Input can be messy: "0532 123 45 67" or "532-123-4567"
$formatted = Turkiye::formatPhoneNumber('0532 123 45 67');

// Output depends on 'phone_format' config:
// 'E164':     "+905321234567" (Default)
// 'NATIONAL': "05321234567"
// 'RAW':      "5321234567"

$masked = Turkiye::maskIdentityNumber('12345678901');

// Output (based on 'mask_char'): "123******01"

use Laravingo\TurkiyeValidator\Utilities\TurkishSanitizer;

$sanitizer = new TurkishSanitizer();

// Title Case (Correctly handles i/İ/I/ı)
echo $sanitizer->toTitle('i̇stanbul ve IĞDIR'); 
// Output: "İstanbul Ve Iğdır"

// Clean Phone Number (Returns pure 10 digits)
echo $sanitizer->cleanPhone('0 (555) 123-45 67');
// Output: "5551234567"

// Clean IBAN (Uppercase + No Spaces)
echo $sanitizer->cleanIban('tr 12 34 56...');
// Output: "TR123456..."

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Laravingo\TurkiyeValidator\Casts\TurkishPhoneCast;
use Laravingo\TurkiyeValidator\Casts\TurkishIbanCast;
use Laravingo\TurkiyeValidator\Casts\TurkishTitleCast;

class User extends Model
{
    protected $casts = [
        'phone'      => TurkishPhoneCast::class, // Auto-cleans to 10 digits
        'iban'       => TurkishIbanCast::class,  // Uppercase + No Spaces
        'full_name'  => TurkishTitleCast::class, // Auto-converts to Title Case (Turkish logic)
    ];
}

// In a Factory or Seeder
$validId    = fake()->turkishIdNumber();    // Valid checksum
$validIban  = fake()->turkishIban();        // Valid TR IBAN
$validTax   = fake()->turkishTaxIdNumber(); // Valid VKN
$validPlate = fake()->turkishLicensePlate();// Valid 06 ABC 123
$validPhone = fake()->turkishPhoneNumber(); // Valid +905...

'locale' => 'tr',
bash
php artisan vendor:publish --tag=turkiye-validator-config