VeldoraDocs

6. ActiveRecord Models & Query Builder

Models extend Veldora\Framework\Database\Model and map directly to database tables.

Creating a Model

Terminal • bash
php veldora make:model Post

app/Models/Post.php
PHP
namespace App\Models;

use Veldora\Framework\Database\Model;
use Veldora\Framework\Database\Relations\BelongsTo;
use Veldora\Framework\Database\Relations\HasMany;

class Post extends Model
{
    // Allowed fields for mass assignment
    protected array $fillable = [
        'title',
        'slug',
        'body',
        'user_id',
        'is_published',
        'published_at',
    ];

    // Automatic type casting
    protected array $casts = [
        'is_published' => 'bool',
        'published_at' => 'datetime',
    ];

    // Hidden from JSON serialization
    protected array $hidden = ['deleted_at'];
}

CRUD Operations

example.php
PHP
// Create
$post = Post::create([
    'title' => 'New Release',
    'slug'  => 'new-release',
    'body'  => 'Veldora 1.0 is here!',
]);

// Find by ID
$post = Post::find(1);

// Find or fail
$post = Post::where('slug', '=', 'new-release')->first();

// Update
$post->title = 'Updated Title';
$post->save();

// Delete
$post->delete();

// Query builder chain
$published = Post::where('is_published', '=', 1)
                 ->where('user_id', '=', 5)
                 ->orderBy('created_at', 'DESC')
                 ->limit(10)
                 ->get();

// Paginate results
$paginator = Post::where('is_published', '=', 1)->paginate(15);


Building with AI Assistants?
Copy the Veldora AI Developer Prompt to teach ChatGPT, Claude, Cursor, or Gemini all framework APIs for error-free coding.