Skip to content

Commit 06b680d

Browse files
committed
🐛 laravel 11 compatibility
1 parent eeeba72 commit 06b680d

File tree

5 files changed

+27
-14
lines changed

5 files changed

+27
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Laravel Rest Api is an elegant way to expose your app through an API, it takes f
66

77
## Requirements
88

9-
PHP 8.1+ and Laravel 10.0+
9+
PHP 8.2+ and Laravel 11+
1010

1111
## Documentation, Installation, and Usage Instructions
1212

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"require": {
1313
"php": "^8.2",
1414
"ext-json": "*",
15-
"laravel/framework": "^11.0|^12.0"
15+
"laravel/framework": "^11|^12"
1616
},
1717
"require-dev": {
1818
"guzzlehttp/guzzle": "^6.0|^7.0",

src/Actions/DispatchAction.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Lomkit\Rest\Actions;
44

55
use Illuminate\Contracts\Queue\ShouldQueue;
6+
use Illuminate\Database\Eloquent\Builder;
67
use Illuminate\Support\Collection;
78
use Illuminate\Support\Facades\Bus;
89
use Illuminate\Support\Facades\DB;
@@ -122,25 +123,36 @@ public function dispatch($chunkCount)
122123
*/
123124
public function handleClassic(int $chunkCount)
124125
{
126+
/**
127+
* @var Builder $searchQuery
128+
*/
125129
$searchQuery =
126130
app()->make(QueryBuilder::class, ['resource' => $this->request->resource, 'query' => null])
127131
->disableDefaultLimit()
128132
->search($this->request->input('search', []));
129133

134+
$limit = $searchQuery->toBase()->limit;
135+
130136
$searchQuery
131137
->clone()
132138
->chunk(
133139
$chunkCount,
134-
function ($chunk) {
135-
return $this->forModels(
136-
\Illuminate\Database\Eloquent\Collection::make(
137-
$chunk
138-
)
139-
);
140+
function ($chunk, $page) use ($limit, $chunkCount) {
141+
$collection = \Illuminate\Database\Eloquent\Collection::make($chunk);
142+
143+
// This is to remove for Laravel 12, chunking with limit does not work
144+
// in Laravel 11
145+
if ($page * $chunkCount >= $limit) {
146+
$collection = $collection->take($limit - ($page - 1) * $chunkCount);
147+
$this->forModels($collection);
148+
return false;
149+
}
150+
151+
return $this->forModels($collection);
140152
}
141153
);
142154

143-
return $searchQuery->getLimit() ?? $searchQuery->count();
155+
return $limit ?? $searchQuery->count();
144156
}
145157

146158
/**
@@ -208,7 +220,7 @@ protected function dispatchSynchronouslyForCollection(Collection $models)
208220
*
209221
* @return $this
210222
*/
211-
protected function addQueuedActionJob(Collection $models)
223+
protected function addQueuedActionJob(Collection $models): self
212224
{
213225
$job = new CallRestApiAction($this->action, $this->fields, $models);
214226

tests/Feature/Controllers/ActionsOperationsTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ public function test_operate_action_with_search(): void
298298
public function test_operate_action_with_search_and_limit(): void
299299
{
300300
ModelFactory::new()
301-
->count(100)
301+
->count(300)
302302
->create([
303303
'string' => 'match',
304304
]);
@@ -315,19 +315,19 @@ public function test_operate_action_with_search_and_limit(): void
315315
'filters' => [
316316
['field' => 'string', 'value' => 'match'],
317317
],
318-
'limit' => 50,
318+
'limit' => 150,
319319
],
320320
],
321321
['Accept' => 'application/json']
322322
);
323323

324324
$response->assertJson([
325325
'data' => [
326-
'impacted' => 50,
326+
'impacted' => 150,
327327
],
328328
]);
329329
$this->assertEquals(
330-
50,
330+
150,
331331
Model::where('number', 100000000)->count()
332332
);
333333
}

tests/Support/Rest/Resources/ModelResource.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ public function limits(RestRequest $request): array
115115
10,
116116
25,
117117
50,
118+
150
118119
];
119120
}
120121

0 commit comments

Comments
 (0)