PHP code example of laragear / rut

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

    

laragear / rut example snippets


use Laragear\Rut\Rut;

$rut = Rut::parse('18.765.432-1');

if ($rut->isValid()) {
    return 'Your RUT is valid!';
}

use Laragear\Rut\Rut;

$rut = new Rut(5138171, 8);

use Laragear\Rut\Rut;

$rut = Rut::parse('5.138.171-8');

use Laragear\Rut\Rut;

Rut::parse('76.482.465-2')->isPermanent(); // "true"

Rut::parse('76.482.465-2')->isTemporal(); // "false"

use Laragear\Rut\Facades\Generator;

$ruts = Generator::make(10);

$rut = Generator::makeOne();

use Laragear\Rut\Facades\Generator;

$people = Generator::asPeople()->make(10);

$companies = Generator::asCompanies()->make(10);

$temporal = Generator::asTemporal()->makeOne();

use Laragear\Rut\Facades\Generator;

$ruts = Generator::unique()->asCompanies()->make(10000000);

use Laragear\Rut\Rut;
use Laragear\Rut\RutFormat;

echo Rut::parse('51381718'); // "5.138.171-8"

echo $rut->formatStrict(); // "5.138.171-8"
echo $rut->formatBasic();  // "5138171-8"
echo $rut->formatRaw();    // "51381718"

use Laragear\Rut\Rut;
use Laragear\Rut\RutFormat;

$rut = Rut::parse('5.138.171-8');

$rut->format();                  // "5.138.171-8"
$rut->format(RutFormat::Strict); // "5.138.171-8"
$rut->format(RutFormat::Basic);  // "5138171-8"
$rut->format(RutFormat::Raw);    // "51381718"

use Laragear\Rut\Rut;

$rut = Rut::parse('5.138.171-8');

if ($rut->isValid()) {
    return "The Rut is valid!";
}

use Laragear\Rut\Rut;

Rut::parse('5.138.171-K')->validate(); // InvalidRutException: "The given RUT is invalid."

use Laragear\Rut\Rut;

if (Rut::check('5.138.171-8')) {
    return "This RUT is valid!";
}

if (Rut::check(5138171, '8')) {
    return "This RUT is also valid!";
}



use Illuminate\Support\Facades\Validator;

$validator = Validator::make([
    'rut' => '14328145-0'
], [
    'rut' => 'rut'
]);

echo $validator->passes(); // true

$validator = Validator::make([
    'rut' => '65.00!!!390XXXX2'
], [
    'rut' => 'rut'
]);

echo $validator->passes(); // true



use Illuminate\Support\Facades\Validator;

$validator = Validator::make([
    'rut' => ['14328145-0', '12.343.580-K', 'thisisnotarut']
], [
    'rut' => 'rut'
]);

echo $validator->passes(); // false

$validator = Validator::make([
    'rut' => ['14328145-0', '12.343.580-K', '20881410-9']
], [
    'rut' => 'rut'
]);

echo $validator->passes(); // true



use Illuminate\Support\Facades\Validator;

$validator = Validator::make([
    'rut' => '14.328.145-0'
], [
    'rut' => 'rut_strict'
]);

echo $validator->passes(); // true

$validator = Validator::make([
    'rut' => '1.4328.145-0'
], [
    'rut' => 'rut_strict'
]);

echo $validator->passes(); // false



use Illuminate\Support\Facades\Validator;

$validator = Validator::make([
    'rut' => ['1.4328.145-0', '12.343.580-K']
], [
    'rut.*' => '



use Illuminate\Support\Facades\Validator;

$validator = Validator::make([
    'rut' => '12.343.580-K'
], [
    'rut' => '



use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

$validator = Validator::make([
    'rut' => [
        'rut_1' => '12.343.580-K',
        'rut_2' => '13.871.792-5',
    ],
], [
    'rut' => [
        '



use Illuminate\Support\Facades\Validator;

$validator = Validator::make([
    'rut' => '12.343.580-K'
], [
    'rut' => '



use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

$validator = Validator::make([
    'rut' => '12.343.580-K',
], [
    'rut' => [
        '



use Illuminate\Support\Facades\Validator;

$validator = Validator::make([
    'rut' => '12.343.580-K'
], [
    'rut' => '



use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

$validator = Validator::make([
    'rut' => '12.343.580-K',
], [
    'rut' => [
        '



use Illuminate\Support\Facades\Validator;

$validator = Validator::make([
    'rut' => '12.343.580-K'
], [
    'rut' => '



use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

$validator = Validator::make([
    'rut' => '12.343.580-K',
], [
    'rut' => [
        '

Schema::create('users', function (Blueprint $table) {
    // $table->unsignedInteger('rut_num');
    // $table->char('rut_vd', 1);
    
    $table->rut();
    
    // ...
});

Schema::create('company', function (Blueprint $table) {
    // $table->unsignedInteger('rut_num')->nullable();
    // $table->char('rut_vd', 1)->nullable();
    
    $table->rutNullable();
    
    // ...
});

Schema::create('users', function (Blueprint $table) {
    // $table->unsignedInteger('rut_num')->primary();
    // $table->char('rut_vd', 1);

    $table->rut()->primary();
    
    // ...
});

use Illuminate\Http\Request;

public function show(Request $request)
{
    $request->validate([
        'person' => '

$request->validate([
    'people'   => 'equest->rut('people');

$request->validate([
    'mom'        => 'children'   => 'est->rut('mom', 'dad'); // Or $request->rut(['mom', 'dad']);
$children = $request->rut('children');



namespace App\Models;

use Laragear\Rut\HasRut;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use HasRut;
    
    // ...
}

use App\Models\User;

$user = User::whereRut('20490006-K')->where('is_active', true)->find();

echo $user->rut; // "20490006-K"

class User extends Authenticatable
{
    use HasRut;
    
    protected const RUT_NUM = 'numero_rut';
    protected const RUT_VD = 'digito_rut';
    
    // ...    
}

/**
 * If the `rut` key should be appended, and hide the underlying RUT columns.
 */
public function shouldAppendRut(): bool
{
    return false;
}

public function shouldAppendRut(): bool
{
   $this->append('rut');

   return false;
}

/**
 * If the primary key of the model should be hidden if it's the RUT Num.
 */
public function shouldShowPrimaryKeyIfIsRutNum(): bool
{
    return false;
}

use Laragear\Rut\RutFormat;

return [
    'format' => RutFormat::Strict,
    'json_format' => null,
    'uppercase' => true,
];

use Laragear\Rut\RutFormat;

return [
    'format' => RutFormat::DEFAULT,
];

use Laragear\Rut\RutFormat;

return [
    'json_format' => null,
];

use Laragear\Rut\Rut;
use Laragear\Rut\RutFormat;

config()->set('rut.format_json', RutFormat::Raw)

Rut::parse('5.138.171-8')->format(); // "5.138.171-8"
Rut::parse('5.138.171-8')->toJson(); // "51381718"

use Laragear\Rut\Rut;

Rut::$jsonFormat = function (Rut $rut) {
    return ['num' => $rut->num, 'vd' => $rut->vd];
}

Rut::parse('5.138.171-8')->toJson(); // "{"num":5138171,"vd":"8"}"

return [
    'uppercase' => true,
];

use Laragear\Rut\RutFormat;
use Laragear\Rut\Rut;

config()->set('rut.uppercase', false)

$rut = Rut::parse('12351839-K');

$rut->format(); // "12.351.839-k"
$rut->toJson(); // "12.351.839-k"
shell
php artisan vendor:publish --provider="Laragear\Rut\RutServiceProvider" --tag="translations"
shell
php artisan vendor:publish --provider="Laragear\Rut\RutServiceProvider" --tag="config"
shell
php artisan vendor:publish --provider="Laragear\Rut\RutServiceProvider" --tag="phpstorm"