Query Builder
The Query class allows you to construct complex SQL queries using a fluent, chainable interface. It handles all parameter binding automatically to prevent SQL Injection.
Retrieving Data
all() vs get()
all(): Executes the query and returns an array of hydrated Model objects (orstdClassif no model is set).get(): Alias forall(), commonly used in Relationships.
first()
Adds LIMIT 1 and returns a single Model instance or null.
cursor() (Memory Efficient)
Uses a PHP Generator to yield one model at a time. Essential for processing thousands of records without running out of RAM.
foreach (User::query()->cursor() as $user) {
// Only one User object exists in memory at a time
process($user);
}
chunk(int $size, callable $cb)
Retrieves records in "pages" of size $size.
Logic Clauses
Where / OrWhere
- Signature:
where(col, value)ORwhere(col, operator, value) - Signature:
where(col, 'IS', null)ORwhere(col, 'IS NOT', null)
WhereIn
WhereExists (Subqueries)
Rubik supports subqueries for existence checks.
$orders = Order::query()->where('total', '>', 500);
// Select Users who have orders > 500
$users = User::whereExists($orders)->all();
Pagination
The paginate() method simplifies frontend integration.
$result = User::where('active', 1)->paginate(page: 2, perPage: 15);
// Structure of $result:
// [
// 'data' => [ ... objects ... ],
// 'total' => 150,
// 'per_page' => 15,
// 'current_page' => 2,
// 'last_page' => 10
// ]
Aggregates
count()
Returns the integer count of rows matching the criteria. It automatically modifies the SELECT clause to COUNT(*) temporarily.
exec()
Returns true if at least one row matches. Optimized to fetch only 1 row.