Skip to content

Commit

Permalink
Update README.md (#26)
Browse files Browse the repository at this point in the history
Add example for case-when in a select
  • Loading branch information
coclav authored Feb 21, 2024
1 parent 41d52c6 commit 7dd973d
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,33 @@ Schema::table('users', function (Blueprint $table) {
});
```

To use the case-when in a `Select` wrap it in an `Alias`:

```php
use Tpetry\QueryExpressions\Language\{CaseGroup, CaseRule};
use Tpetry\QueryExpressions\Language\Alias;
use Tpetry\QueryExpressions\Operator\Comparison\{Equal};
use Tpetry\QueryExpressions\Value\Value;
use App\Models\User;

User::query()
->select([
"id",
new Alias(
new CaseGroup(
when: [
new CaseRule(new Value("Admin"), new Equal("role", new Value(3))),
new CaseRule(new Value("Editor"), new Equal("role", new Value(2))),
new CaseRule(new Value("Viewer"), new Equal("role", new Value(1)))
],
else: new Value("Unknown Role")
),
"role_name"
)
])
->get();
```

### Operators

#### Arithmetic Operators
Expand Down

0 comments on commit 7dd973d

Please sign in to comment.