tighten / ziggy

Use your Laravel routes in JavaScript.
MIT License
3.91k stars 248 forks source link

Route having optional parameters won't works right #663

Closed kokoropie closed 1 year ago

kokoropie commented 1 year ago

Ziggy version

v1.6.2

Laravel version

v10.19.0

Description

I have a route (home) with an optional parameter

Route::get('/{day?}', [HomeController::class, 'index'])->name('home')->where('day', '[0-9]{4}-[0-9]{2}-[0-9]{2}');

when I test route() helper in Laravel it gave me the url I want

route('home', ["day" => "2023-08-22", "id" => 1]); // /2023-08-22?id=1
route('home', ["id" => 1]); // /?id=1

but when i use route() in js it gave me an error instead of

route('home', ["day" => "2023-08-22", "id" => 1]); // /2023-08-22?id=1
route('home', ["id" => 1]); // Uncaught Error: Ziggy error: 'day' parameter does not match required format '[0-9]{4}-[0-9]{2}-[0-9]{2}' for route 'home'.

Ziggy call and context

route('home', {id: 1});

Ziggy configuration

{
    "url": "http://127.0.0.1",
    "port": null,
    "defaults": {},
    "routes": {
        "debugbar.openhandler": {
            "uri": "_debugbar/open",
            "methods": [
                "GET",
                "HEAD"
            ]
        },
        "debugbar.clockwork": {
            "uri": "_debugbar/clockwork/{id}",
            "methods": [
                "GET",
                "HEAD"
            ]
        },
        "debugbar.assets.css": {
            "uri": "_debugbar/assets/stylesheets",
            "methods": [
                "GET",
                "HEAD"
            ]
        },
        "debugbar.assets.js": {
            "uri": "_debugbar/assets/javascript",
            "methods": [
                "GET",
                "HEAD"
            ]
        },
        "debugbar.cache.delete": {
            "uri": "_debugbar/cache/{key}/{tags?}",
            "methods": [
                "DELETE"
            ]
        },
        "home": {
            "uri": "{day?}",
            "methods": [
                "GET",
                "HEAD"
            ],
            "wheres": {
                "day": "[0-9]{4}-[0-9]{2}-[0-9]{2}"
            }
        },
        "profile.edit": {
            "uri": "profile",
            "methods": [
                "GET",
                "HEAD"
            ]
        },
        "profile.update": {
            "uri": "profile",
            "methods": [
                "PATCH"
            ]
        },
        "profile.destroy": {
            "uri": "profile",
            "methods": [
                "DELETE"
            ]
        },
        "teacher.index": {
            "uri": "teacher",
            "methods": [
                "GET",
                "HEAD"
            ]
        },
        "teacher.store": {
            "uri": "teacher",
            "methods": [
                "POST"
            ]
        },
        "teacher.update": {
            "uri": "teacher/{teacher}",
            "methods": [
                "PUT",
                "PATCH"
            ],
            "bindings": {
                "teacher": "teacher_id"
            }
        },
        "teacher.destroy": {
            "uri": "teacher/{teacher}",
            "methods": [
                "DELETE"
            ],
            "bindings": {
                "teacher": "teacher_id"
            }
        },
        "subject.index": {
            "uri": "subject",
            "methods": [
                "GET",
                "HEAD"
            ]
        },
        "subject.store": {
            "uri": "subject",
            "methods": [
                "POST"
            ]
        },
        "subject.update": {
            "uri": "subject/{subject}",
            "methods": [
                "PUT",
                "PATCH"
            ],
            "bindings": {
                "subject": "subject_id"
            }
        },
        "subject.destroy": {
            "uri": "subject/{subject}",
            "methods": [
                "DELETE"
            ],
            "bindings": {
                "subject": "subject_id"
            }
        },
        "schedule.index": {
            "uri": "schedule",
            "methods": [
                "GET",
                "HEAD"
            ]
        },
        "schedule.store": {
            "uri": "schedule",
            "methods": [
                "POST"
            ]
        },
        "schedule.update": {
            "uri": "schedule/{schedule}",
            "methods": [
                "PUT",
                "PATCH"
            ],
            "bindings": {
                "schedule": "schedule_id"
            }
        },
        "schedule.destroy": {
            "uri": "schedule/{schedule}",
            "methods": [
                "DELETE"
            ],
            "bindings": {
                "schedule": "schedule_id"
            }
        },
        "schedule.detail.store": {
            "uri": "schedule/{schedule}/detail",
            "methods": [
                "POST"
            ],
            "bindings": {
                "schedule": "schedule_id"
            }
        },
        "schedule.detail.update": {
            "uri": "schedule/{schedule}/detail/{detail}",
            "methods": [
                "PUT",
                "PATCH"
            ],
            "bindings": {
                "schedule": "schedule_id",
                "detail": "schedule_detail_id"
            }
        },
        "schedule.detail.destroy": {
            "uri": "schedule/{schedule}/detail/{detail}",
            "methods": [
                "DELETE"
            ],
            "bindings": {
                "schedule": "schedule_id",
                "detail": "schedule_detail_id"
            }
        },
        "register": {
            "uri": "register",
            "methods": [
                "GET",
                "HEAD"
            ]
        },
        "login": {
            "uri": "login",
            "methods": [
                "GET",
                "HEAD"
            ]
        },
        "password.confirm": {
            "uri": "confirm-password",
            "methods": [
                "GET",
                "HEAD"
            ]
        },
        "password.update": {
            "uri": "password",
            "methods": [
                "PUT"
            ]
        },
        "logout": {
            "uri": "logout",
            "methods": [
                "POST"
            ]
        }
    }
}

Route definition

Route::get('/{day?}', [HomeController::class, 'index'])->name('home')->where('day', '[0-9]{4}-[0-9]{2}-[0-9]{2}');
kokoropie commented 1 year ago

ah, read document again and know how to use query, it's just don't like Laravel helper