Tools to parse, validate and generate Chilean RUT in Laravel.
use Laragear\Rut\Rut;
$rut = Rut::parse('18.765.432-1');
if ($rut->isValid()) {
return 'Your RUT is valid!';
}
Your support allows me to keep this package free, up-to-date and maintainable. Alternatively, you can spread the word!
- Laravel 10 or later
Fire up Composer and require it into your project:
composer require laragear/rut
To create a RUT from an already valid source, instance a Rut
object with the numbers and the verification digit, separately.
use Laragear\Rut\Rut;
$rut = new Rut(5138171, 8);
Otherwise, you may want to use parse()
to create it from a single string. It will try its best to create a RUT instance from what is given, or throw an InvalidRutException
if the string doesn't have the necessary characters to create a RUT.
use Laragear\Rut\Rut;
$rut = Rut::parse('5.138.171-8');
Officially, there are six types of RUT. To differentiate between them, you have access to is...()
methods.
RUT Type | From | To | Type Check |
---|---|---|---|
Person | 100.000 |
45.999.999 |
isPerson() |
Foreign Investor Person | 46.000.000 |
47.999.999 |
isInvestor() |
Foreign Investor Company | 47.000.000 |
47.999.999 |
isInvestorCompany() |
Contingency | 48.000.000 |
59.999.999 |
isContingency() |
Company | 60.000.000 |
99.999.999 |
isCompany() |
Temporal | 100.000.000 |
199.999.999 |
isTemporal() |
Additionally, you have access to the isPermanent()
method, which checks if the RUT is below 100.000.000.
use Laragear\Rut\Rut;
Rut::parse('76.482.465-2')->isPermanent(); // "true"
Rut::parse('76.482.465-2')->isTemporal(); // "false"
Important
This package considers RUT as valid when between 100.000 and 200.000.000, inclusive. Most (if not all) people using 99.999 or lower RUT numbers are deceased.
The package comes with a convenient RUT Generator
facade to create thousands or millions of random RUTs using fluid methods.
The make()
method generates a Collection of 15 Rut
by default, but you can set any number you want. Alternatively, you can use makeOne()
to create just one random Rut
.
use Laragear\Rut\Facades\Generator;
$ruts = Generator::make(10);
$rut = Generator::makeOne();
You can use as...()
to make a given type of RUTs.
use Laragear\Rut\Facades\Generator;
$people = Generator::asPeople()->make(10);
$companies = Generator::asCompanies()->make(10);
$temporal = Generator::asTemporal()->makeOne();
If you plan to create several millions of RUTs, there is a high change you will come with duplicates. To avoid collisions, use the unique()
method in exchange for a small performance hit to remove duplicates.
use Laragear\Rut\Facades\Generator;
$ruts = Generator::unique()->asCompanies()->make(10000000);
By default, all Rut
instances are serialized into text using a strict format. You can serialize a Rut
instance differently using one of the three formats available:
Formatting | Enum | Example | Description |
---|---|---|---|
Strict | RutFormat::Strict |
5.138.171-8 |
Default option. Serializes with a thousand separator and hyphen. |
Basic | RutFormat::Basic |
5138171-8 |
No thousand separator, only the hyphen. |
Raw | RutFormat::Raw |
51381718 |
No thousand separator nor hyphen. |
You can use format()
with any RutFormat
enum as argument to serialize the RUT into text.
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"
You may change this globally in the configuration.
You should use the included Validation Rules to validate RUTs in your input.
Otherwise, you can manually validate a RUT using isValid()
or isInvalid()
to check if it's mathematically valid or not, respectively.
use Laragear\Rut\Rut;
$rut = Rut::parse('5.138.171-8');
if ($rut->isValid()) {
return "The Rut is valid!";
}
Using the validate()
method will throw a InvalidRutException
if it's invalid.
use Laragear\Rut\Rut;
Rut::parse('5.138.171-K')->validate(); // InvalidRutException: "The given RUT is invalid."
You can also validate RUT strings directly, or an already separated RUT, by using check()
method.
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!";
}
All validation rules messages can be translated. You can add your own translation to these rules by publishing the translation files:
php artisan vendor:publish --provider="Laragear\Rut\RutServiceProvider" --tag="translations"
This checks if the RUT being passed is a valid RUT string. This automatically cleans the RUT from anything except numbers and the verification digit. Only then it checks if the resulting RUT is mathematically valid.
<?php
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
This may come handy in situations when the user presses a wrong button into an RUT input, so there is no need to ask the user to properly format a RUT. Afterwards, you can use the Request RUT helpers to retrieve the RUT from the Request input or query.
The rule also accepts an array
of RUTs. In that case, rut
will succeed if all the RUTs are valid. This may come in handy when a user is registering a lot of people into your application.
<?php
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
This works the same as rut
, but it will validate RUTs that are also using the Strict RUT format: with a thousand separator and a hyphen before the Validation Digit.
It will return false
even if there is one misplaced character or an invalid one.
<?php
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
This rule also accepts an array
of RUTs. In that case, rut_strict
will return true if all the RUTs are properly formatted and valid.
<?php
use Illuminate\Support\Facades\Validator;
$validator = Validator::make([
'rut' => ['1.4328.145-0', '12.343.580-K']
], [
'rut.*' => 'required|rut_strict',
]);
echo $validator->paases(); // false
Instead of using Laravel's exists, you can use rut_exists
in case your database has separated columns for the RUT Number and Verification Digit.
For this to work you need to set the table to look for, the RUT number column and RUT verification digit column, otherwise the rule will guess the column names by the attribute key and appending _num
and _vd
, respectively.
This rule automatically validates the RUT before doing the query.
<?php
use Illuminate\Support\Facades\Validator;
$validator = Validator::make([
'rut' => '12.343.580-K'
], [
'rut' => 'required|rut_exists:mysql.users,rut_num,rut_vd'
]);
echo $validator->passes(); // false
Since this also checks if the RUT is valid (not strict), it will fail if it's not, or the RUT doesn't exist in the database.
To customize the query, you can use the Rule
class of Laravel with the method rutExists
. Note that you can input the number and verification digit columns, or both, if you don't want to let the rule guess them, as it may incorrectly guess when using a wildcard.
<?php
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' => [
'required',
Rule::rutExists('mysql.users', 'rut_num', 'rut_vd')->where('account_id', 1),
]
]);
echo $validator->passes(); // true
Tip
Database rules will normalize the verification digit as uppercase in the database for search queries.
This validation rule checks if only the number of the RUT exists, without taking into account the verification digit. This is handy when the Database has an index in the number of the RUT, thus making this verification blazing fast.
This rule automatically validates the RUT before doing the query.
<?php
use Illuminate\Support\Facades\Validator;
$validator = Validator::make([
'rut' => '12.343.580-K'
], [
'rut' => 'required|num_exists:mysql.users,rut_num'
]);
echo $validator->passes(); // false
You can customize the underlying query using the numExists
.
<?php
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
$validator = Validator::make([
'rut' => '12.343.580-K',
], [
'rut' => [
'required',
Rule::numExists('mysql.users', 'rut_num')->where('account_id', 1),
]
]);
echo $validator->passes(); // false
This works the same as the rut_exists
rule, but instead of checking if the RUT exists in the Database, it will detect if it doesn't. This rule works just like the Laravel's unique
rule works.
This rule automatically validates the RUT before doing the query.
<?php
use Illuminate\Support\Facades\Validator;
$validator = Validator::make([
'rut' => '12.343.580-K'
], [
'rut' => 'required|rut_unique:mysql.users,rut_num,rut_vd'
]);
echo $validator->passes(); // false
You can also exclude a certain ID or records from the Unique validation. For this, you need to use the Rule
class.
<?php
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
$validator = Validator::make([
'rut' => '12.343.580-K',
], [
'rut' => [
'required',
Rule::rutUnique('mysql.users', 'rut_num')->ignore(request()->user()),
]
]);
echo $validator->passes(); // false
Tip
Database rules will normalize the verification digit as uppercase in the database for search queries.
Caution
You should never pass any user controlled request input into the ignore method. Instead, you should only pass a system generated unique ID such as an auto-incrementing ID or UUID from an Eloquent model instance. Otherwise, your application will be vulnerable to an SQL injection attack.
This rule will check only if the number of the RUT doesn't exists already in the database, which is useful for Databases with an index solely on the number of the RUT. This rule also matches the Laravel's unique
rule works.
This rule automatically validates the RUT before doing the query.
<?php
use Illuminate\Support\Facades\Validator;
$validator = Validator::make([
'rut' => '12.343.580-K'
], [
'rut' => 'required|num_unique:mysql.users,rut_num'
]);
echo $validator->passes(); // false
You can also exclude a certain ID or records from the Unique validation. For this, you need to use the Rule
class.
<?php
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
$validator = Validator::make([
'rut' => '12.343.580-K',
], [
'rut' => [
'required',
Rule::numUnique('mysql.users')->ignore(request()->user()->id),
]
]);
echo $validator->passes(); // false
Tip
Database rules will normalize the verification digit in the database for search queries.
Caution
You should never pass any user controlled request input into the ignore method. Instead, you should only pass a system generated unique ID such as an auto-incrementing ID or UUID from an Eloquent model instance. Otherwise, your application will be vulnerable to an SQL injection attack.
If you're creating your database from the ground up, you don't need to manually create the RUT columns. Just use the rut()
or rutNullable()
helpers in the Blueprint:
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();
// ...
});
Tip
The rutNullable()
method creates both Number and Verification Digit columns as nullable.
If you plan to use the RUT Number as an index, which may speed up queries to look for RUTs, you can just index the Number column by fluently adding primary()
, index()
or unique()
depending on your database needs. This is because it has more performance sense to index only the Number rather than the whole RUT.
Schema::create('users', function (Blueprint $table) {
// $table->unsignedInteger('rut_num')->primary();
// $table->char('rut_vd', 1);
$table->rut()->primary();
// ...
});
This package includes the rut()
macro helper for the Request
instance, which retrieves a single RUT from an input or query.
use Illuminate\Http\Request;
public function show(Request $request)
{
$request->validate([
'person' => 'required|rut'
]);
$rut = $request->rut('person');
// ...
}
If the input is iterable, like an array
or even a Collection
instance, you will receive a Collection
of Rut
instances.
$request->validate([
'people' => 'required|array',
'people.*' => 'rut'
]);
$ruts = $request->rut('people');
You can also retrieve multiple keys from the Request, which will also return a Collection
.
$request->validate([
'mom' => 'required|rut',
'dad' => 'required|rut',
'children' => 'required|array'
'children.*' => 'required|rut',
]);
$parents = $request->rut('mom', 'dad'); // Or $request->rut(['mom', 'dad']);
$children = $request->rut('children');
Important
It's imperative you validate your input before retrieving RUTs. If there is a malformed RUT, an exception will be thrown.
This package contains the HasRut
trait to use in Laravel Eloquent Models with tables that have separate RUT Number and RUT Verification digit.
This trait conveniently adds a RUT Scope to a model that has a RUT in its columns, and the rut
property which returns a Rut
instance.
<?php
namespace App\Models;
use Laragear\Rut\HasRut;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use HasRut;
// ...
}
With that, you will have access to convenient RUT queries shorthands:
Method name | Description |
---|---|
findRut() |
Finds a record by the given RUT. |
findManyRut() |
Finds many records by the given RUTs. |
findRutOrFail() |
Finds a record by the RUT or fails. |
findRutOrNew() |
Finds a record by the RUT or creates one. |
whereRut() |
Creates a WHERE clause with the RUT number equal to the issued one. |
whereRutNot() |
Creates a WHERE clause excluding the given RUT. |
orWhereRut() |
Creates a OR WHERE clause with the RUT number equal to the issued one. |
orWhereRutNot() |
Creates a OR WHERE clause excluding the given RUT. |
whereRutIn() |
Creates a WHERE IN clause with the given RUTs. |
whereRutNotIn() |
Creates a WHERE NOT IN clause excluding the given RUTs. |
orWhereRutIn() |
Creates a OR WHERE IN clause with the given RUTs. |
orWhereRutNotIn() |
Creates a OR WHERE NOT IN clause excluding the given RUTs. |
Important
These RUT queries work over the RUT Number for convenience, as the RUT Verification Digit should be verified only on persistence.
These scopes can be used in your queries easily:
use App\Models\User;
$user = User::whereRut('20490006-K')->where('is_active', true)->find();
The rut
property is dynamically created from the RUT Number and RUT Verification Digit columns, which uses a Cast underneath.
echo $user->rut; // "20490006-K"
By convention, the trait uses rut_num
and rut_vd
as the default columns to retrieve and save the RUT Number and RUT Verification Digit, respectively.
You can easily change it to anything your database is working with for the given Model:
class User extends Authenticatable
{
use HasRut;
protected const RUT_NUM = 'numero_rut';
protected const RUT_VD = 'digito_rut';
// ...
}
RUT Appended and columns hidden
By default, the rut
property is appended, and the underlying columns containing the RUT information are hidden. This enables compatibility with Livewire real-time validation.
{
"id": 1,
"name": "Taylor",
"email": "[email protected]",
"rut": "16.887.941-5"
}
To show the underlying RUT columns instead of the RUT string, simply make shouldAppendRut()
in the model to return false
.
/**
* If the `rut` key should be appended, and hide the underlying RUT columns.
*
* @return bool
*/
public function shouldAppendRut(): bool
{
return false;
}
This will effectively return both columns as normal properties.
{
"id": 1,
"name": "Taylor",
"email": "[email protected]",
"rut_num": 16887941,
"rut_vd": "5"
}
If you need to make the rut
key and the underlying columns visible, you may override the shouldAppendRut()
method and return false
.
public function shouldAppendRut(): bool
{
$this->append('rut');
return false;
}
This package works flawlessly out of the box, but you may want to change how a Rut
is formatted as a string using the global configuration. You can publish it using Artisan:
php artisan vendor:publish --provider="Laragear\Rut\RutServiceProvider" --tag="config"
You will receive the config/rut.php
config file like this:
use Laragear\Rut\RutFormat;
return [
'format' => RutFormat::Strict,
'json_format' => null,
'uppercase' => true,
];
use Laragear\Rut\RutFormat;
return [
'format' => RutFormat::DEFAULT,
];
By default, RUTs are strictly formatted. This config alters how RUTs are serialized as string in your application globally.
use Laragear\Rut\RutFormat;
return [
'json_format' => null,
];
For the case of JSON, RUT are cast as a string using the global format when this is null
. You can set any format to use when serializing into JSON exclusively.
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"
Alternatively, you can override the configuration by using a callback to create your own JSON format. The callback accepts the Rut
instance, and it should return an array
or a string
to be serialized into JSON. A good place to put this logic is in the boot()
method of your AppServiceProvider
file.
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,
];
Since the Verification Digit can be either a single digit or the letter K
, it's usually good idea to keep the case consistent; to always work with uppercase or lowercase across all the application.
The Rut
instance by default will use uppercase K
, but you can change it to lowercase globally by setting this to false
. This will affect all Rut
instances.
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"
Tip
This doesn't affect database rules, as the verification digit is normalized automatically.
For users of PhpStorm, there is a stub file to aid in macro autocompletion for this package. You can publish it using the phpstorm
tag:
php artisan vendor:publish --provider="Laragear\Rut\RutServiceProvider" --tag="phpstorm"
The file gets published into the .stubs
folder of your project. You should point your PhpStorm to these stubs.
- There are no singletons using a stale application instance.
- There are no singletons using a stale config instance.
- There are no singletons using a stale request instance.
Rut
static properties are only written once at boot time from config.
There should be no problems using this package with Laravel Octane.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
This specific package version is licensed under the terms of the MIT License, at time of publishing.
Laravel is a Trademark of Taylor Otwell. Copyright © 2011-2024 Laravel LLC.