Skip to content

Conversation

@techmahedy
Copy link
Member

@techmahedy techmahedy commented Jan 20, 2026

This PR introduces five new ORM helper methods to improve query readability, reduce boilerplate, and support common data-access patterns

New Methods Added

  • validate() - Allows inline validation of model attributes directly from a query, returning valid and invalid records in a structured way.
  • sample() - Fetches a random subset of records from a query, useful for testing, previews, or analytics sampling.
  • withAge() - Dynamically calculates the age of a record based on a timestamp column and exposes it as a computed property.
  • tap() - Enables side-effects (logging, metrics, debugging) on query results without modifying the returned collection.
  • parallel() - Executes multiple scoped queries in parallel from a single model, returning grouped results for cleaner and more expressive data access.

validate()

Validate model attributes directly from a query and separate valid and invalid records.

['valid' => $valid, 'invalid' => $invalid] = User::validate([
    'email' => fn ($v) => filter_var($v, FILTER_VALIDATE_EMAIL),
    'name'  => 'required',
]);

return $invalid

sample()

Retrieve a random subset of records from a model.

$sample = User::sample(10); // usefull for big data

withAge()

Compute the age of records dynamically based on a timestamp column.

$tickets = Ticket::query()
    ->where('status', 'open')
    ->withAge('created_at', 'hours');

foreach ($tickets as $ticket) {
    if ($ticket->age > 24) {
        echo "Ticket #{$ticket->id} is {$ticket->age} hours old";
    }
}

tap()

Execute side effects (logging, debugging, metrics) without modifying the query result.

$users = User::query()
    ->where('status', 1)
    ->tap(function ($collection) {
        info("Found {$collection->count()} active users");
    });

parallel()

Run multiple queries in parallel and return grouped results.

$results = User::parallel([
    'admins' => fn ($q) => $q->where('role', 'admin'),
    'users'  => fn ($q) => $q->where('role', 'editor'),
]);

@techmahedy techmahedy merged commit 78c39f0 into doppar:3.x Jan 20, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant