Add validation for query

This commit is contained in:
NoAvatar 2024-10-18 02:15:24 +10:00
parent b4c44f5a14
commit ae59f826e9

View File

@ -4,6 +4,8 @@
use App\Models\User; use App\Models\User;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
class UserController extends Controller class UserController extends Controller
{ {
@ -16,6 +18,15 @@ public function index()
// GET /user/{id} // GET /user/{id}
public function show(string $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); $user = User::query()->findOrFail($id);
return response($user); return response($user);