PHP code example of liar88828 / laravel-schema-attributes

1. Go to this page and download the library: Download liar88828/laravel-schema-attributes 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/ */

    

liar88828 / laravel-schema-attributes example snippets


Schema::create('orders', function (Blueprint $table) {
    $table->id();
    $table->foreignId('user_id')->constrained()->cascadeOnDelete();
    $table->string('status', 20)->default('pending')->index();
    $table->decimal('total', 10, 2);
    $table->timestamps();
    $table->softDeletes();
});

#[EloquentModel(model: Order::class)]
#[Table(name: 'orders', timestamps: true, softDeletes: true)]
class OrderSchema
{
    #[PrimaryKey(type: 'bigIncrements')]
    public int $id;

    #[Column(type: 'unsignedBigInteger', index: true)]
    #[ForeignKey(references: 'id', on: 'users', onDelete: 'cascade')]
    // #[BelongsTo(related: UserSchema::class)]  ← set the related schema
    // #[Fillable]
    public int $user_id;

    #[Column(type: 'string', length: 20, default: 'pending', index: true)]
    // #[Fillable]
    public string $status;

    #[Column(type: 'decimal', precision: 10, scale: 2)]
    // #[Fillable]
    public float $total;
}

#[EloquentModel(model: Article::class)]
#[Table(name: 'articles', timestamps: true, softDeletes: true)]
class ArticleSchema
{
    #[PrimaryKey(type: 'bigIncrements')]
    public int $id;

    #[Column(type: 'string', length: 191)]
    #[Fillable] #[Required] #[Min(2)] #[Max(191)]
    public string $title;

    #[Column(type: 'unsignedBigInteger')]
    #[ForeignKey(references: 'id', on: 'users', onDelete: 'cascade')]
    #[BelongsTo(related: UserSchema::class)]
    public int $user_id;
}

use Illuminate\Foundation\Auth\User;
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;

#[EloquentModel(
    model:  UserModel::class,
    extend: User::class,
    traits: [Notifiable::class, TwoFactorAuthenticatable::class],
)]
#[Table(name: 'users', timestamps: true, softDeletes: true)]
class UserSchema
{
    #[Column(type: 'string', length: 191, unique: true)]
    #[Fillable] #[Required] #[Email] #[Unique(table: 'users', column: 'email')]
    public string $email;

    #[Column(type: 'string')]
    #[Fillable] #[Hidden] #[Cast('hashed')] #[Required] #[Confirmed]
    public string $password;

    #[Column(type: 'rememberToken')]
    #[Hidden]
    public ?string $remember_token = null;
}

// Schema-driven validation
User::schemaValidateOrFail($request->all());

// Update validation (ignores own unique fields)
$user->schemaValidateForUpdate($request->all(), ignoreUniqueFor: ['email' => $user->id]);

// Auto $fillable, $hidden, $casts, $table from schema
// UUID primary key auto-generation
// Relation resolution from schema annotations
bash
php artisan schema:refresh ArticleSchema