Skip to content

Commit 1fa2669

Browse files
[9.x] Allow loading trashed models for resource routes (#44405)
* allow loading trashed models for resource routes This change gives the ability to load trashed models when registering a route resource. You can load trashed models on **all** the resource routes by calling `withTrashed()` with no parameters: ```php Route::resource('users', UserController::class)->withTrashed(); ``` You can selectively load trashed models on methods by passing an array: ```php Route::resource('users', UserController::class)->withTrashed(['show']); ``` * minor styling * minor formatting oops * formatting Co-authored-by: Taylor Otwell <[email protected]>
1 parent b7af7ff commit 1fa2669

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/Illuminate/Routing/PendingResourceRegistration.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,19 @@ public function scoped(array $fields = [])
227227
return $this;
228228
}
229229

230+
/**
231+
* Define which routes should allow "trashed" models to be retrieved when resolving implicit model bindings.
232+
*
233+
* @param array $methods
234+
* @return \Illuminate\Routing\PendingResourceRegistration
235+
*/
236+
public function withTrashed(array $methods = [])
237+
{
238+
$this->options['trashed'] = $methods;
239+
240+
return $this;
241+
}
242+
230243
/**
231244
* Register the resource route.
232245
*

src/Illuminate/Routing/ResourceRegistrar.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ public function register($name, $controller, array $options = [])
9494

9595
$collection = new RouteCollection;
9696

97-
foreach ($this->getResourceMethods($defaults, $options) as $m) {
97+
$resourceMethods = $this->getResourceMethods($defaults, $options);
98+
99+
foreach ($resourceMethods as $m) {
98100
$route = $this->{'addResource'.ucfirst($m)}(
99101
$name, $base, $controller, $options
100102
);
@@ -103,6 +105,11 @@ public function register($name, $controller, array $options = [])
103105
$this->setResourceBindingFields($route, $options['bindingFields']);
104106
}
105107

108+
if (isset($options['trashed']) &&
109+
in_array($m, ! empty($options['trashed']) ? $options['trashed'] : $resourceMethods)) {
110+
$route->withTrashed();
111+
}
112+
106113
$collection->add($route);
107114
}
108115

0 commit comments

Comments
 (0)