Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

trifek's avatar

Validation request in Laravel API

Hi, I have problem with Laravel Request validation in Laravel 9.

I have api.php (route):

Route::get('date/{date}', [HomeController::class, 'date']);

HomeController:

public function date(DateRequest $request, int $date)
    {
        dd($date);
    }

and DateRequest:

class DateRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, mixed>
     */
    public function rules()
    {
        return [
            'date' => 'required',
        ];
    }

when I open in browser url: localhost/api/date/2022-12-12 - I have redirect to main page.

When I remove from Controller: DateRequest $request or in DataRequest I'm remove 'randomNumber' => 'required',- it's work fine

What's wrong?

0 likes
5 replies
MichalOravec's avatar

With localhost/api/date/2022-12-12?date=something it will work. Guess why?

trifek's avatar

@MichalOravec I'm try something like this: Validation:

return [
            'resultDate' => 'required|date|date_format:d.m.Y',
        ];

api.php:

Route::get('date/{resultDate}', [HomeController::class, 'date']);

and it's not help

trifek's avatar

@MichalOravec

Ok, I make this code with 2 tests: 1. api.php

Route::get('date/{resultDate}', [HomeController::class, 'date'])->name('showByDate')->where('resultDate', '([0-9]{2}).([0-9]{2}).([0-9]{4})+');

validation:

'resultDate' => 'required|date|date_format:d.m.Y',

and this is not working (redirect)

in validation I remove: 'resultDate' => 'required|date|date_format:d.m.Y',

and them it's working.

Why it's not working?

In date actualy I can write: 13.25.2022 - and it's working :/

Please or to participate in this conversation.