Skip to content

Commit 9a0aa69

Browse files
authored
[10.x] Add string capability to withTrashed method (#46356)
* add string ability to method of `withTrashed` method * add ImplicitController for testing `withTrashed` method * add testSoftDeletedModelsCanBeRetrievedUsingWithTrashedMethodWithSpecificFunction * fix styleci
1 parent 7d436e0 commit 9a0aa69

File tree

2 files changed

+87
-2
lines changed

2 files changed

+87
-2
lines changed

src/Illuminate/Routing/PendingResourceRegistration.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,15 @@ public function scoped(array $fields = [])
230230
/**
231231
* Define which routes should allow "trashed" models to be retrieved when resolving implicit model bindings.
232232
*
233-
* @param array $methods
233+
* @param array|string $methods
234234
* @return \Illuminate\Routing\PendingResourceRegistration
235235
*/
236-
public function withTrashed(array $methods = [])
236+
public function withTrashed(array|string $methods = [])
237237
{
238+
if (is_string($methods)) {
239+
$methods = (array) $methods;
240+
}
241+
238242
$this->options['trashed'] = $methods;
239243

240244
return $this;

tests/Integration/Routing/ImplicitModelRouteBindingTest.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,72 @@
77
use Illuminate\Database\Eloquent\Model;
88
use Illuminate\Database\Eloquent\SoftDeletes;
99
use Illuminate\Database\Schema\Blueprint;
10+
use Illuminate\Http\Request;
11+
use Illuminate\Routing\Controller;
1012
use Illuminate\Support\Facades\Route;
1113
use Illuminate\Support\Facades\Schema;
1214
use Orchestra\Testbench\Concerns\InteractsWithPublishedFiles;
1315
use Orchestra\Testbench\TestCase;
1416

17+
class ImplicitController extends Controller
18+
{
19+
/**
20+
* Display a listing of the resource.
21+
*/
22+
public function index()
23+
{
24+
//
25+
}
26+
27+
/**
28+
* Show the form for creating a new resource.
29+
*/
30+
public function create()
31+
{
32+
//
33+
}
34+
35+
/**
36+
* Store a newly created resource in storage.
37+
*/
38+
public function store(Request $request)
39+
{
40+
//
41+
}
42+
43+
/**
44+
* Display the specified resource.
45+
*/
46+
public function show(ImplicitBindingUser $user)
47+
{
48+
return $user;
49+
}
50+
51+
/**
52+
* Show the form for editing the specified resource.
53+
*/
54+
public function edit(ImplicitBindingUser $user)
55+
{
56+
//
57+
}
58+
59+
/**
60+
* Update the specified resource in storage.
61+
*/
62+
public function update(Request $request, ImplicitBindingUser $user)
63+
{
64+
//
65+
}
66+
67+
/**
68+
* Remove the specified resource from storage.
69+
*/
70+
public function destroy(ImplicitBindingUser $user)
71+
{
72+
//
73+
}
74+
}
75+
1576
class ImplicitModelRouteBindingTest extends TestCase
1677
{
1778
use InteractsWithPublishedFiles;
@@ -141,6 +202,26 @@ public function testSoftDeletedModelsCanBeRetrievedUsingWithTrashedMethod()
141202
]);
142203
}
143204

205+
public function testSoftDeletedModelsCanBeRetrievedUsingWithTrashedMethodWithSpecificFunction()
206+
{
207+
$user = ImplicitBindingUser::create(['name' => 'Dries']);
208+
209+
$user->delete();
210+
211+
config(['app.key' => str_repeat('a', 32)]);
212+
213+
Route::resource('users', ImplicitController::class)
214+
->middleware(['web'])
215+
->withTrashed('show');
216+
217+
$response = $this->getJson("/users/{$user->id}");
218+
219+
$response->assertJson([
220+
'id' => $user->id,
221+
'name' => $user->name,
222+
]);
223+
}
224+
144225
public function testEnforceScopingImplicitRouteBindings()
145226
{
146227
$user = ImplicitBindingUser::create(['name' => 'Dries']);

0 commit comments

Comments
 (0)