---
title: "Query scopes — Credits for Laravel | Roundly"
description: "Report over the ledger with the Credit model’s scopes — grants, deductions, point in time, owner and bucket filters."
url: https://roundly-consulting.com/open-source/docs/credits-for-laravel/query-scopes
language: en
---

[All packages](https://roundly-consulting.com/open-source.md)

[Credits for Laravel](https://roundly-consulting.com/open-source/docs/credits-for-laravel.md)

# Query scopes

The Credit model ships query scopes for reporting over the ledger:

```php
use RoundlyConsulting\Credits\Models\Credit;

Credit::query()->grants()->get();                               // amount > 0
Credit::query()->deductions()->get();                           // amount < 0
Credit::query()->upTo(now()->subWeek())->get();                 // created_at <= $at
Credit::query()->forCreditable($user)->get();                   // rows owned by $user
Credit::query()->bucket('promotional')->get();                  // rows in a named bucket
Credit::query()->buckets(['promotional', 'purchased'])->get();  // rows in any listed bucket
```

| Scope | Filters to |
| --- | --- |
| `grants()` | amount > 0 |
| `deductions()` | amount < 0 |
| `upTo($at)` | created\_at <= $at — $at is an instant, moved to app.timezone (the zone created\_at is stored in) before it is bound. |
| `forCreditable($model)` | Rows owned by the given model (whereMorphedTo). |
| `bucket($name)` | Rows in one named bucket. |
| `buckets($names)` | Rows in any of the listed buckets. |

## Composing scopes

Scopes chain with each other and with the creditable’s credits() relation:

```php
// Scopes compose — and work on the relation too.
Credit::query()->forCreditable($user)->bucket('promotional')->grants()->sum('amount');

$user->credits()->deductions()->upTo(now()->subMonth())->latest()->get();
```

The model casts amount to int and meta to array, and exposes the owner through the creditable() MorphTo relation.

## Show your open-source love

This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.

[More ways to support, including crypto](https://roundly-consulting.com/support-us.md)

By donating, you agree to our [donation terms](https://roundly-consulting.com/donation-terms.md).

[Support our open source work (opens in a new tab)](https://donate.stripe.com/dRmeVe8FX5PF1Qd9pXcEw00) [Join us on Patreon (opens in a new tab)](https://www.patreon.com/cw/roundly)

## Want this built into your product?

We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.

[Get a quote in 48 hours](https://roundly-consulting.com/contact.md) [Browse all packages](https://roundly-consulting.com/open-source/docs/credits-for-laravel.md)
