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

yassin98010's avatar

how to pass date into route

Hello I have this route :

Route::get('dateValues/{date}','HydrosolutionWebService@getDateValues')

i want that my date with this format dd.mm.yyyy

how can i do plz ? THNK

0 likes
7 replies
martinbean's avatar

@yassin98010 I usually have the date components separate, like this:

Route::get('events/{year}/{month}/{day}/{slug}', 'EventController@show');
bobbybouwmann's avatar

To go further then @martinbean you can then handle it like this in your controller

public function show($year, $month, $day, $slug)
{
    // Do your thing here
}
martinbean's avatar

@bobbybouwmann I tend to resolve the event model before passing it to the controller so my URL parameters don’t have to match the method signature, i.e.

Route::get('event/{year}/{month}/{day}/{slug}', function ($year, $month, $day, $slug) {
    // This would normally be done in route service provider
    $event = app('App\Event')->whereYear('start_date', '=', $year)
                             ->whereYear('start_date', '=', $year)
                             ->whereMonth('start_date', '=', $month)
                             ->whereDay('start_date', '=', $day)
                             ->whereSlug($slug)
                             ->firstOrFail();

    return app('App\Http\Controllers\EventController')->show($event);
});
public function show(Event $event)
{
    return view('event.show', compact('event'));
}

This way, if I change my URLs (i.e. /events/{id}), I don’t have to change my controller methods. All it’s expecting is an Event instance, it doesn’t matter how it gets it as that’s the responsibility of the router.

bobbybouwmann's avatar

That's a good solution as well, but if you have a small application you won't notice anything!

Also another use case where my example would be useful if you have a year and a month view as well

Route::get('events/{year?}/{month?}/{day?}', 'EventController@show')

public function show($year = null, $month = null, $day = null)
{
    // Show the results based on the parameters
    // If they are not set, show all events

    if ($day != null && $month != null && $year != null)
    {
        return Event::whereYear('start_date', '=', $year)
            ->whereMonth('start_date', '=', $month)
            ->whereDay('start_date', '=', $day)
            ->get();
    }

    // If everything else fails, return all events
    return Event::all();

Note: this code can use a lot of improvements, but you get the idea!

tomwurzbach's avatar

I'm late to the party, but here's how I solved this:

In your RouteServiceProvider, check and convert the date into a Carbon. This has the benefit of not passing an invalid date to your controller. We use Ymd format, but you can change this to whatever.

    public function boot()
    {
        $this->bind( 'dateof', function( $dateof ) {

            $carbon_date = Carbon::createFromFormat( 'Ymd', $dateof );
            if ( $carbon_date->format( 'Ymd' ) === $dateof ) return $carbon_date;
            throw new NotFoundHttpException();
        });

        parent::boot();
    }

Now, your web.php looks like this and any {dateof} named parameter is handled cleanly.

Route::get( '/prelim/{dateof}', 'Accounting\Controller@show' );

and your controller looks like this:

    public function show( $date )
    {
    // and the $date will be a well-formatted Carbon
    }
5 likes

Please or to participate in this conversation.