From 87430a11cf1036fcb61fc69c65710be4af63319a Mon Sep 17 00:00:00 2001 From: Colin Claverie Date: Wed, 21 Feb 2024 09:51:58 +0000 Subject: [PATCH] Update README.md Add example for case-when in a select --- README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/README.md b/README.md index 1c41dcf..154eaca 100644 --- a/README.md +++ b/README.md @@ -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