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

boyjarv's avatar

How to change date in URL?

So my URL is currently set to show todays date:

http://moveme.test/venue/1152522/2020-09-23/tagin/stats

I want the URL to change to the selected date from the form below:

<form action="{{route('venue.venuetaginstats', [Auth::user()->venue_id, date('Y-m-d')])}}">
        <select name="taginDate" id="taginDate">
        @foreach($data as $date)
                <option value="{{$date->taginDate}}">
                    {{ Carbon\Carbon::parse($date->taginDate)->format('l jS \of F Y') }}
                </option>
        @endforeach
        </select>
        <input type="submit" value="search">
    </form>

but when I submit the form, I get:

http://moveme.test/venue/1152522/2020-09-23/tagin/stats?taginDate=2020-09-04
0 likes
23 replies
jlrdw's avatar

Where are you getting taginDate, seems you are bypassing it via:

<form action="{{route('venue.venuetaginstats', [Auth::user()->venue_id, date('Y-m-d')])}}">

Get your request in a controller.

boyjarv's avatar

taginDate is the name of the select

MichalOravec's avatar

In value save whole url and with javascript change action attribute. This is in pure js.

<form id="date-form" action="{{ route('venue.venuetaginstats', [Auth::user()->venue_id, today()->toDateString()]) }}">
    <select name="taginDate" id="taginDate">
        @foreach($data as $date)
            <option value="{{ route('venue.venuetaginstats', [Auth::user()->venue_id, $date->taginDate]) }}">
                {{ Carbon\Carbon::parse($date->taginDate)->format('l jS \of F Y') }}
            </option>
        @endforeach
    </select>

    <input type="submit" value="search">
</form>

<script>
    document.getElementById('date-form').taginDate.onchange = function() {
        document.getElementById('date-form').action = this.value;
    };
</script>
boyjarv's avatar

Thanks but that didn't work?!

I got

http://moveme.test/venue/1152522/2020-09-23/tagin/stats?taginDate=http%3A%2F%2Fmoveme.test%2Fvenue%2F1152522%2F2020-09-17%2Ftagin%2Fstats
jlrdw's avatar

You are getting what you pass

date('Y-m-d')

Somewhere you have to request taginDate.

boyjarv's avatar

I just need the URL to change:

from this:

http://moveme.test/venue/1152522/2020-09-23/tagin/stats

and if someone selects the 17th

http://moveme.test/venue/1152522/2020-09-17/tagin/stats
MichalOravec's avatar

Ok I changed it little bit. This should work

<form id="date-form" action="{{ route('venue.venuetaginstats', [Auth::user()->venue_id, today()->toDateString()]) }}">
    <select id="taginDate">
        @foreach($data as $date)
            <option value="{{ route('venue.venuetaginstats', [Auth::user()->venue_id, $date->taginDate]) }}">
                {{ Carbon\Carbon::parse($date->taginDate)->format('l jS \of F Y') }}
            </option>
        @endforeach
    </select>

    <input type="submit" value="search">
</form>

<script>
    document.getElementById('taginDate').onchange = function() {
        document.getElementById('date-form').action = this.value;
    };
</script>
boyjarv's avatar

Thanks, that doesn't work, it just adds a '?' to the end of the URL

MichalOravec's avatar

Ok last try

<form id="date-form" action="{{ route('venue.venuetaginstats', [Auth::user()->venue_id, today()->toDateString()]) }}">
    <select id="taginDate">
        @foreach($data as $date)
            <option value="{{ route('venue.venuetaginstats', [Auth::user()->venue_id, $date->taginDate]) }}">
                {{ Carbon\Carbon::parse($date->taginDate)->format('l jS \of F Y') }}
            </option>
        @endforeach
    </select>

    <input type="submit" value="search">
</form>

<script>
    document.getElementById('taginDate').onchange = function() {
        document.getElementById('date-form').action = this.value;
    };

    document.getElementById('date-form').onsubmit = function() {
        window.location = this.action;
    
        return false;
    };
</script>

Working example you can see here http://jsfiddle.net/43vhk6Lu/3/

Open devtools and under network tab you can see that it works.

boyjarv's avatar

no, but thanks anyways.... anyone else?

MichalOravec's avatar

Other people just give you similar answer and they maybe use jquery, vue.js and so on. What means that you have to use javascript for that.

boyjarv's avatar

don't have to use jquery or javascript, would just like it to work, thanks so much for your help so far

boyjarv's avatar

not sure why it doesn't work MichaelOravec?!

jlrdw's avatar

Route to a controller and get the request there. Or use a button and set up a click event that gets the data change in the JavaScript. The way you are doing it now you never get the data from the select option.

boyjarv's avatar

by default I arrive at the page with todays date, when I select a different date, it just adds a '?' to the end of the URL...

here is my code so far (well MichaelOravec's code):


    <form id="date-form" action="{{ route('venue.venuetaginstats', [Auth::user()->venue_id, today()->toDateString()]) }}">
        <select id="taginDate" onchange="this.form.submit()">
            @foreach($data as $date)
                <option value="{{ route('venue.venuetaginstats', [Auth::user()->venue_id, $date->taginDate]) }}">
                    {{ Carbon\Carbon::parse($date->taginDate)->format('l jS \of F Y') }}
                </option>
            @endforeach
        </select>
    </form>

    <script>
        document.getElementById('taginDate').onchange = function() {
            document.getElementById('date-form').action = this.value;
        };

        document.getElementById('date-form').onsubmit = function() {
            window.location = this.action;

            return false;
        };
    </script>
boyjarv's avatar

jlrdw are you suggesting I change my route

Route::get('venue/{id}/{now}/tagin/stats', 'VenueController@venueTaginstats')->name('venue.venuetaginstats');

and my controller?

public function venueTaginstats($id, $date) {

    $tagins = Tagin::latest()->where('venue_id',$id)->where('created_at', 'like', '%' . $date . '%')->paginate(1000);
    $data = Tagin::select(DB::raw('DATE_FORMAT(created_at, "%Y-%m-%d") as taginDate'))->distinct()->where('venue_id',$id)->get();


        $thevenue = Venue::findOrFail($id);

        return view('venues.tagins', compact(
            'tagins','thevenue', 'data'));
    }
jlrdw's avatar

In the controller request taginDate if selected, now you have a default or taginDate to deal with choose which one and use in the query. If no selection was made I'm guessing you want to use your default date.

Also for a date, you shouldn't need like, wouldn't "=" work.

boyjarv's avatar

I got it working:

controller:

public function venueTaginstats($id, Request $request) {

        $date = $request->get('taginDate');
        if($date == ''){
            $date = date('Y-m-d');
        }
        $tagins = Tagin::latest()->where('venue_id',$id)->where('created_at', 'like', '%' . $date . '%')->paginate(1000);
        $data = Tagin::select(DB::raw('DATE_FORMAT(created_at, "%Y-%m-%d") as taginDate'))->distinct()->where('venue_id',$id)->get();
        $thevenue = Venue::findOrFail($id);

        return view('venues.tagins', compact(
            'tagins','thevenue', 'data'));
    }

route:

Route::get('venue/{id}/tagin/stats', 'VenueController@venueTaginstats')->name('venue.venuetaginstats');
boyjarv's avatar

I added that in instead of the button, I tried with the button as in your code first... Thanks so much for your help

thewebartisan7's avatar
Level 14

Why use a form with select if you just need to switch an url? Create a dropdown with links...

jlrdw's avatar

@boyjarv that's what I meant in my first reply, when I said get the request in the controller. That way you choose what date is needed.

What did you end up doing,

  • Getting request in controller?
  • dropdown with links?

Please or to participate in this conversation.