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

mateoo88's avatar

Date parametr from the URL

In my application, I pass a date parameter from the URL, for example: mypage.com/panel/2024-05-23. Later, I store this date into a variable and use the variable for a database query. I wanted to write the code in such a way that the hyphens would disappear from the date in the URL. However, just for fun, I checked: mypage.com/panel/20240523, and Laravel automatically matches the date from the URL without any problems, so the query works correctly. Why does this happen?

0 likes
4 replies
martinbean's avatar

@mateoo88 Why do you want to remove the hyphens? If you’re intending to use this in an SQL query then you would need the hyphens, since that’s how values are stored in date columns in most popular SQL databases.

If you want to enforce the format, then you should use Route::pattern to define a regular expression. I’ve done this in the past for things like date parameters.

mateoo88's avatar

@martinbean I wanted to remove the hyphens from the URL for aesthetic reasons - in my opinion, the URL looks nicer without them. Later, I wanted to restore them in the controller to prepare the full date with hyphens for the SQL query.

I'm asking because I didn't do anything, yet when I enter the date without hyphens in the URL, my query still works correctly, as if Laravel automatically changes it.

Snapey's avatar

@mateoo88 depends on your code. If you have casts for this field it is probably being parsed by Carbon before being used in your eloquent query. Just depends how your query is written.

Laravel does not know its a date, so, no, its not doing anything automatically.

mateoo88's avatar

@Snapey My query looks like this:

public function show($date = null)
    {
        $datum = $date ?? DB::table('job')->max('date');
		$query = DB::table('job')
            ->join('details', 'job.id', '=', 'details.job_id')
            ->where('job.date',  $datum)
			->select('job.date', 'job.name', 'details.place')
			->get();

and my web.php looks like this: Route::get('/panel/{date?}', [PanelController::class, 'show'])->name('panel.index');

I don't use Carbon anywhere in these places

Now I noticed that when my address looks like this, for example: page.com/panel/20240523sometext, it still displays the correct data according to the date. Strange.

Please or to participate in this conversation.