Use this skill when creating migrations for Models that require one or many RUT columns, and query these models by their RUT. Do not use it outside Eloquent Models, or on external APIs.
php artisan boost:add-skill laragear/rut
Save this content to: .claude/skills/eloquent/SKILL.md
A Model with a RUT requires a special RUT column and a trait to operate. Models also can support multiple RUTs, but must be tackled using an especial Attribute for each one.
## Set up migrations with RUT
- Do not use `unsignedInteger()` for RUT. Instead, ensure the target Model migration contains the necessary RUT columns by using `$table->rut()`.
- When a RUT column requires to be nullable, use `$table->rutNullable()`.
- The `$table->rut()` creates `rut_num` and `rut_vd` columns. When a name is given, that name is used as a canonical base, e.g. `$table->rut('parent')` = `$table->unsignedInteger('parent_num'); $table->char('parent_vd', 1);`.
- When the model requires handling more than one RUT column, the `rut()` method can be used with different names to avoid collisions, e.g. `$table->rut('child')`, `$table->rut('parent')`, etc.
- Add `->index()` if the RUT is expected to be part of search queries, likely to find records matching the RUT.
```php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
// ...
$table->rut(); // Add the RUT number and verification digit columns as `rut_num` and `rut_vd`.
$table->rut('child'): // Adds `child_num` and `child_vd` columns.
$table->rut('business')->index(); // Adds the RUT columns and indexes the `business_num` column.
$table->rutNullable('parent'); // Adds nullable RUT columns as `parent_num` and `parent_vd`.
});
}
}
```
## Set up a Model with RUT
Locate the model that will use single RUTs and add the `Laragear\Rut\HasRut` trait.
```php
use Illuminate\Database\Eloquent\Model;
use Laragear\Rut\HasRut;
class User extends Model
{
use HasRut;
// ...
}
```
If the model uses non-standard column names for RUT (e.g. `$this->rut('issuer')`), point out the RUT Number and Verification Digit constants in the Model:
```php
class User extends Model
{
use HasRut;
public const string RUT_NUM = 'issuer_num';
public const string RUT_VD = 'issuer_vd';
// ...
}
```
### RUT Local Scopes
When using the `Laragear\Rut\HasRut` trait in the model, the model query builder will have access to many RUT Local Scopes. Read the PHPDoc in the source code of the `Laragear\Rut\HasRut` trait for an updated list of available methods to properly query the RUT column.
```php
use App\Models\User;
// Find the user by the exact RUT
$user = User::findByRut('15.518.258-K');
// Filters the query by a given RUT
$users = User::whereRut('15.518.258-K')->active()->limit(10)->get();
// Check for users with similar RUT given
$users = User::whereRutLike('158')->take(5)->get();
```
### Multiple RUTs in a Model
The `HasRut` trait only supports **one RUT** per model as the default in the entire model, which the local scopes are used against. When dealing with secondary RUTs, use the `RutAttribute` with the canonical base of the RUT columns:
```php
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Laragear\Rut\Eloquent\RutAttribute;
use Laragear\Rut\Rut;
/**
* @propery \Laragear\Rut\Rut $child_rut
*/
class User extends Model
{
protected function issuer(): Attribute
{
// Automatically creates a get/set for "issuer_num" and "issuer_vd" (`$table->rut('issuer')`).
return RutAttribute::for('issuer');
}
protected function seller(): Attribute
{
// Creates a RUT get/set for "seller_num" (`$table->unsignedInteger('seller_num')`).
return RutAttribute::forNum('seller_rut_number');
}
}
```
The RUT can then be set and retrieved in the model using the `rut` attribute. Set accepts `Rut` instances, integers (e.g. `111111111`), strings (e.g. `22.222.222-2`). It always returns a `Laragear\Rut\Rut` instance when there is a rut.
```php
use App\Models\User;
$rut = User::find(1)->rut;
```
A Model with a RUT requires a special RUT column and a trait to operate. Models also can support multiple RUTs, but must be tackled using an especial Attribute for each one.
unsignedInteger() for RUT. Instead, ensure the target Model migration contains the necessary RUT columns by using $table->rut().$table->rutNullable().$table->rut() creates rut_num and rut_vd columns. When a name is given, that name is used as a canonical base, e.g. $table->rut('parent') = $table->unsignedInteger('parent_num'); $table->char('parent_vd', 1);.rut() method can be used with different names to avoid collisions, e.g. $table->rut('child'), $table->rut('parent'), etc.->index() if the RUT is expected to be part of search queries, likely to find records matching the RUT.use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
// ...
$table->rut(); // Add the RUT number and verification digit columns as `rut_num` and `rut_vd`.
$table->rut('child'): // Adds `child_num` and `child_vd` columns.
$table->rut('business')->index(); // Adds the RUT columns and indexes the `business_num` column.
$table->rutNullable('parent'); // Adds nullable RUT columns as `parent_num` and `parent_vd`.
});
}
}
Locate the model that will use single RUTs and add the Laragear\Rut\HasRut trait.
use Illuminate\Database\Eloquent\Model;
use Laragear\Rut\HasRut;
class User extends Model
{
use HasRut;
// ...
}
If the model uses non-standard column names for RUT (e.g. $this->rut('issuer')), point out the RUT Number and Verification Digit constants in the Model:
class User extends Model
{
use HasRut;
public const string RUT_NUM = 'issuer_num';
public const string RUT_VD = 'issuer_vd';
// ...
}
When using the Laragear\Rut\HasRut trait in the model, the model query builder will have access to many RUT Local Scopes. Read the PHPDoc in the source code of the Laragear\Rut\HasRut trait for an updated list of available methods to properly query the RUT column.
use App\Models\User;
// Find the user by the exact RUT
$user = User::findByRut('15.518.258-K');
// Filters the query by a given RUT
$users = User::whereRut('15.518.258-K')->active()->limit(10)->get();
// Check for users with similar RUT given
$users = User::whereRutLike('158')->take(5)->get();
The HasRut trait only supports one RUT per model as the default in the entire model, which the local scopes are used against. When dealing with secondary RUTs, use the RutAttribute with the canonical base of the RUT columns:
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Laragear\Rut\Eloquent\RutAttribute;
use Laragear\Rut\Rut;
/**
* @propery \Laragear\Rut\Rut $child_rut
*/
class User extends Model
{
protected function issuer(): Attribute
{
// Automatically creates a get/set for "issuer_num" and "issuer_vd" (`$table->rut('issuer')`).
return RutAttribute::for('issuer');
}
protected function seller(): Attribute
{
// Creates a RUT get/set for "seller_num" (`$table->unsignedInteger('seller_num')`).
return RutAttribute::forNum('seller_rut_number');
}
}
The RUT can then be set and retrieved in the model using the rut attribute. Set accepts Rut instances, integers (e.g. 111111111), strings (e.g. 22.222.222-2). It always returns a Laragear\Rut\Rut instance when there is a rut.
use App\Models\User;
$rut = User::find(1)->rut;
How can I help you explore Laravel packages today?