Skip to content

Commit

Permalink
Merge pull request #28 from patrickomeara/main
Browse files Browse the repository at this point in the history
Add docs for conditional queries and convenience meethods
  • Loading branch information
freekmurze authored Dec 11, 2024
2 parents 62d70cb + 2f749a4 commit 9b89084
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
21 changes: 21 additions & 0 deletions docs/php/laravel/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ return [
*/
'slow_query_threshold_in_ms' => env('RAY_SLOW_QUERY_THRESHOLD_IN_MS', 500),

/*
* When enabled, all update queries will automatically be sent to Ray.
*/
'send_update_queries_to_ray' => env('SEND_UPDATE_QUERIES_TO_RAY', false),

/*
* When enabled, all insert queries will automatically be sent to Ray.
*/
'send_insert_queries_to_ray' => env('SEND_INSERT_QUERIES_TO_RAY', false),

/*
* When enabled, all delete queries will automatically be sent to Ray.
*/
'send_delete_queries_to_ray' => env('SEND_DELETE_QUERIES_TO_RAY', false),

/*
* When enabled, all select queries will automatically be sent to Ray.
*/
'send_select_queries_to_ray' => env('SEND_SELECT_QUERIES_TO_RAY', false),

/*
* When enabled, all requests made to this app will automatically be sent to Ray.
*/
Expand Down Expand Up @@ -89,6 +109,7 @@ return [
/*
* The host used to communicate with the Ray app.
* When using Docker on Mac or Windows, you can replace localhost with 'host.docker.internal'
* When using Docker on Linux, you can replace localhost with '172.17.0.1'
* When using Homestead with the VirtualBox provider, you can replace localhost with '10.0.2.2'
* When using Homestead with the Parallels provider, you can replace localhost with '10.211.55.2'
*/
Expand Down
35 changes: 35 additions & 0 deletions docs/php/laravel/queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,41 @@ $users = ray()->showQueries(function (): Illuminate\Support\Collection {
User::all(); // this query won't be displayed.
```

## Conditional queries

You may want to only show queries if a certain condition is met. You can pass a closure that will receive `QueryExecuted` to `showConditionalQueries`.

```php
// When a binding contains 'joan', the query will be displayed.
ray()->showConditionalQueries(fn (QueryExecuted $query) =>
Arr::first(
$query->bindings,
fn ($binding) => Str::contains($binding, 'joan')
)
);
```

This is particularly helpful when dealing with many queries during migrations or data manipulation.

Convenience methods are available for select, insert, update, and delete queries.

```php
ray()->showInsertQueries();
// Insert queries will be displayed.
ray()->stopShowingInsertQueries();

// Update queries will be displayed during execution of handleUpdate().
ray()->showUpdateQueries(fn () => $this->handleUpdate());

// Select queries will be displayed.
ray()->showSelectQueries();

// Delete queries will be displayed.
ray()->showDeleteQueries();
```

Additionally, these can be enabled in the config file.

## Counting queries

If you're interested in how many queries a given piece of code executes, and what the runtime of those queries is, you can use `countQueries`. It expects you to pass a closure in which all the executed queries will be counted.
Expand Down

0 comments on commit 9b89084

Please sign in to comment.