From ae59f826e9d292ad04c5f9f03d9e0bb4cc353a67 Mon Sep 17 00:00:00 2001 From: NoAvatar Date: Fri, 18 Oct 2024 02:15:24 +1000 Subject: [PATCH] Add validation for query --- app/Http/Controllers/UserController.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 7699545..a6ddfe8 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -4,6 +4,8 @@ use App\Models\User; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Validator; +use Symfony\Component\HttpFoundation\Exception\BadRequestException; class UserController extends Controller { @@ -16,6 +18,15 @@ public function index() // GET /user/{id} public function show(string $id) { + // FIXME: is there more shorter + // validation solution + $v = Validator::make(["id" => $id], [ + "id" => 'integer' + ]); + + if ($v->fails()) + throw new BadRequestException(); + $user = User::query()->findOrFail($id); return response($user);