Since you are sending it as a GET you can simply do
$date = $request->query('date');
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have never worked with datepickers before and I am struggling with something that might be very simple
I am building a calendar and I want to allow users to navigate to any given day chosen in a datepicker.
My code is like that:
The method in the controller:
public function show(Company $company, $date = null)
{
$date = Carbon::parse($date);
}
The route:
Route::get('{company}/{date?}', 'SalonsController@show')->name('salon.show');
The form in HTML
<form action="{{route('salon.show', [$company, 'date'])}}" method="GET">
{{ csrf_field() }}
{{ method_field('GET') }}
<label class="input tag form-control">
<input name="date" type="date" value="date" id="date">
<button class="btn heute text-center" type="submit">Senden</button>
</label>
</form>
Let's say I select the Nov. 27, 2019 in the date picker. The I see the URL something like this:
http://project.test/company/date?_token=wlUxABTbnZy6jeAbAwxDVVs9JdxkycUx7GBTx4GE&_method=GET&date=2019-11-27
Note that 'date' is passed in the URL, behind the company, so the routing is working properly sending $company and 'date'.
But I get the following error:
DateTime::__construct(): Failed to parse time string (date) at position 0 (d): The timezone could not be found in the database
It is logic since I'm pasing a string in the form. Here -> <form action="{{route('salon.show', [$company, 'date'])}}" method="GET">
But if I change to a variable $date. That variable does not exist and I get an error.
The question is.
How do I pack the selected date from the datepicker in a variable and send it to the controller?
Form fields are always strings. You need to clear up a few things.
Change your form to POST
remove the {{method_field}}
Remove the /{date?}/ from the route since it cannot have any effect
Remove the $date from the controller method signature
change controller to correctly parse the date string in the request
The method in the controller:
public function show(Request $request, Company $company)
{
$date = Carbon::createFromFormat('Y-m-d', $request->date);
}
The route:
Route::post('{company}', 'SalonsController@show')->name('salon.show');
The form in HTML
<form action="{{route('salon.show', $company)}}" method="POST">
{{ csrf_field() }}
<label class="input tag form-control">
<input name="date" type="date" value="date" id="date">
<button class="btn heute text-center" type="submit">Senden</button>
</label>
</form>
Please or to participate in this conversation.