Skip to content

Commit

Permalink
Update README.md
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 87430a1
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`. Example:

```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 87430a1

Please sign in to comment.