Skip to content

Commit 9f5dca2

Browse files
authored
Merge pull request #31 from omaratpxt/feat-add-with-archived-route-instruction
feat: add withArchived route instruction
2 parents 7a14534 + 20b3a1b commit 9f5dca2

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ $onlyArchivedUsers = User::query()->onlyArchived();
7676

7777
By default, the global scope of this trait uses the `withoutArchived` extension when the trait is added to a model.
7878

79+
#### Archived models in route implicit binding
80+
Typically, implicit model binding will not retrieve models that have been archived. However, you may instruct the implicit binding to retrieve these models by chaining the withArchived method onto your route's definition:
81+
82+
```php
83+
use App\Models\User;
84+
85+
Route::get('/users/{user}', function (User $user) {
86+
return $user->email;
87+
})->withArchived();
88+
```
89+
7990
### Testing
8091

8192
```composer test```

src/Archivable.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,4 +206,22 @@ public function getQualifiedArchivedAtColumn()
206206
{
207207
return $this->qualifyColumn($this->getArchivedAtColumn());
208208
}
209+
210+
/**
211+
* Resolve the bindings for a given route.
212+
* @see \Illuminate\Database\Eloquente\Model::resolveRouteBinding
213+
*
214+
* @return \Illuminate\Database\Eloquent\Model
215+
*/
216+
public function resolveRouteBinding($value, $field = null)
217+
{
218+
if (
219+
request()->route()->allowsArchivedBindings()
220+
) {
221+
return $this->resolveRouteBindingQuery($this, $value, $field)
222+
->withoutGlobalScopes([ArchivableScope::class])->first();
223+
}
224+
225+
return parent::resolveRouteBinding($value, $field);
226+
}
209227
}

src/LaravelArchivableServiceProvider.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Database\Schema\Blueprint;
66
use Illuminate\Support\ServiceProvider;
7+
use Illuminate\Routing\Route;
78

89
class LaravelArchivableServiceProvider extends ServiceProvider
910
{
@@ -31,6 +32,16 @@ protected function configureMacros()
3132
Blueprint::macro('dropArchivedAt', function ($column = 'archived_at') {
3233
return $this->dropColumn($column);
3334
});
35+
36+
Route::macro('withArchived', function (bool $withArchived = true) {
37+
$this->withArchivedBindings = $withArchived;
38+
39+
return $this;
40+
});
41+
42+
Route::macro('allowsArchivedBindings', function () {
43+
return $this->withArchivedBindings ?? false;
44+
});
3445
}
3546

3647
/**

tests/ArchivableRouteTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace LaravelArchivable\Tests;
4+
5+
use LaravelArchivable\Tests\TestClasses\ArchivableModel;
6+
use Illuminate\Support\Facades\Route;
7+
8+
class ArchivableRouteTest extends TestCase
9+
{
10+
/** @test */
11+
public function fail_if_archived_model_is_found()
12+
{
13+
14+
Route::get('/archivable-models/{archivableModel}', function (ArchivableModel $archivableModel) {
15+
return $archivableModel;
16+
})->middleware(\Illuminate\Routing\Middleware\SubstituteBindings::class);
17+
18+
$model = ArchivableModel::factory()->archived()->create();
19+
20+
21+
$this->get('/archivable-models/'.$model->getKey())
22+
->assertNotFound();
23+
}
24+
25+
26+
/** @test */
27+
public function it_finds_archived_model_when_withArchived_is_used()
28+
{
29+
Route::get('/archivable-models/{archivableModel}', function (ArchivableModel $archivableModel) {
30+
return $archivableModel;
31+
})->middleware(\Illuminate\Routing\Middleware\SubstituteBindings::class)->withArchived();
32+
33+
$model = ArchivableModel::factory()->archived()->create();
34+
35+
36+
$this->get('/archivable-models/'.$model->getKey())
37+
->assertOk();
38+
}
39+
}

0 commit comments

Comments
 (0)