RafaelMunoznl
7 months ago
364
6
General

Pack a selected date from the datepicker in a variable and send it to the controlle

Posted 7 months ago by RafaelMunoznl

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?}', '[email protected]')->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?

Please sign in or create an account to participate in this conversation.