For simpler workflows, use these convenience methods on the model:
creditsByMetadata(string $key, mixed $value, int $limit = null, string $order = 'desc', string $operator = '='): A shorthand for querying and limiting metadata-filtered transactions.creditHistoryWithMetadata(array $filters, int $limit = null): Allows passing an array of filter definitions.
Filter Array Syntax for creditHistoryWithMetadata:
- Two-parameter style:
['key' => '...', 'value' => '...'] (uses equality). - Three-parameter style:
['key' => '...', 'operator' => '...', 'value' => '...'] (allows custom operators like > or <). - Null handling: To check for null, explicitly include
'value' => null in the array.
// Get credits filtered by metadata (with limit and ordering)
$purchases = $user->creditsByMetadata('source', 'purchase', limit: 20, order: 'desc');
// With comparison operators
$highValue = $user->creditsByMetadata('order_value', '>=', 100, limit: 10);
// Multiple metadata filters
$filtered = $user->creditHistoryWithMetadata([
['key' => 'source', 'value' => 'purchase'],
['key' => 'category', 'value' => 'electronics'],
['key' => 'tags', 'value' => 'premium', 'method' => 'contains'],
['key' => 'order_value', 'operator' => '>', 'value' => 50],
], limit: 25);
// Query with null values
$nullChecks = $user->creditHistoryWithMetadata([
['key' => 'refund_id', 'operator' => '=', 'value' => null],
['key' => 'status', 'value' => 'pending'],
], limit: 10);