Skip to content

Commit 1a1ed92

Browse files
committed
Add affordance to restore queries while preparing response
1 parent 67c4653 commit 1a1ed92

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/Illuminate/Foundation/Application.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,4 +1444,17 @@ public function preventQueriesWhilePreparingResponse($connection = null)
14441444

14451445
return $this;
14461446
}
1447+
1448+
/**
1449+
* Restores ability for database to query while preparing the request response.
1450+
*
1451+
* @param string|null $connection
1452+
* @return $this
1453+
*/
1454+
public function allowQueriesWhilePreparingResponse($connection = null)
1455+
{
1456+
$this['events']->listen(PreparingResponse::class, fn () => $this['db']->connection($connection)->allowQueries());
1457+
1458+
return $this;
1459+
}
14471460
}

tests/Integration/Foundation/ApplicationTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,31 @@ public function toArray($request)
9797

9898
$this->assertCount(1, DB::getQueryLog());
9999
}
100+
101+
public function testItcanRestoreValue()
102+
{
103+
Schema::create('foo', function ($table) {
104+
$table->id();
105+
});
106+
Route::get('test-route', function () {
107+
return new class('xxxx') extends JsonResource {
108+
public function toArray($request)
109+
{
110+
return [
111+
//
112+
];
113+
}
114+
};
115+
})->middleware(TestMiddleware::class);
116+
DB::enableQueryLog();
117+
$this->withoutExceptionHandling();
118+
119+
$this->app->preventQueriesWhilePreparingResponse();
120+
$this->app->allowQueriesWhilePreparingResponse();
121+
$this->get('test-route')->assertOk();
122+
123+
$this->assertCount(1, DB::getQueryLog());
124+
}
100125
}
101126

102127
class TestMiddleware

0 commit comments

Comments
 (0)