Laravel - Implicit route model binding with soft deleted data
PHP Snippet 1:
//Route Service Provider
$router->bind('post', function($post)
return Post::withTrashed()->where('id', $post)->firstOrFail();
});
// Controller
public function show(Post $post) {
// If the post has been trashed and the user is not admin, he cannot see it
if (!Auth::user()->isAdmin() && $post->trashed())
abort(404);
// Proceed with normal request because the post has not been deleted.
}
PHP Snippet 2:
Route::get('posts/{post}', [PostController::class, 'show'])->withTrashed();