-
Notifications
You must be signed in to change notification settings - Fork 11.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[8.x] Added onlyTrashed to routes as an addition to withTrashed #38462
Conversation
No plans to add this right now. |
This seems really useful for a trashed route. Route::patch('/documents/{document}/trashed', function (Document $document) {
$document->restore();
return 'OK';
})->middleware(['web'])->onlyTrashed();
Route::delete('/documents/{document}/trashed', function (Document $document) {
$document->forceDelete();
return '';
})->middleware(['web'])->onlyTrashed();
|
was the main reason for me of adding this. This would also avoid checking if a model is soft deleted in every public function destroy(int $invoice)
{
$invoice = Invoice::onlyTrashed()->findOrFail( $invoice );
$invoice->forceDelete()
} |
@SuperDJ You could create a middleware instead that checks if a parameter is an eloquent model then check if it's trashed. Route::delete('/documents/{document}/trashed', function (Document $document) {
$document->forceDelete();
return '';
})->middleware(['web'])->onlyTrashed(); would change to Route::delete('/documents/{document}/trashed', function (Document $document) {
$document->forceDelete();
return '';
})->middleware(['web', 'trashed']); |
@dennisprudlo But how to write the middleware if there is many diff model with use softDelete Route added ->onlyTrashed() is useful just like we can use otherwise we need add many extra code for check the model is deleted in many place |
@tommy66374 Wouldn't this be sufficient? class Trashed
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
foreach ($request->route()->parameters() as $resolved) {
if ($resolved instanceof Model && !$resolved->trashed()) {
abort(403);
}
}
return $next($request);
}
} Then add that middleware to the routes Route::get('/', [ XyzController::class, 'index' ])->middleware(\App\Http\Middleware\Trashed::class); |
@dennisprudlo This is probably not sufficient for nested controllers, e.g.
because @taylorotwell After reading about this new, great 'withTrashed' feature I was really hoping to see that there was already an accompanying 'onlyTrashed' method, mainly because of what @goodevilgenius already said: some actions should not be done on non-trashed models. |
@martenkoetsier Of course the middleware can be altered to add the desired functionality. This was just a simple example. In fact this middleware would throw an exception if you route model bind an eloquent model that doesn't use soft deletes. After If the |
Added
onlyTrashed
to routes as an addition towithTrashed
.Usage: