@yassin98010 I usually have the date components separate, like this:
Route::get('events/{year}/{month}/{day}/{slug}', 'EventController@show');
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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
@yassin98010 I usually have the date components separate, like this:
Route::get('events/{year}/{month}/{day}/{slug}', 'EventController@show');
It would probably be easier to format the date as yyyy-mm-dd using the toDateString() function in Carbon.
http://carbon.nesbot.com/docs/#api-formatting
And to use Carbon in any file just pull it in by putting use Carbon\Carbon; at the top.
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
}
@bobbybouwmann i don't understand your solution ?
@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.
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!
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
}
Please or to participate in this conversation.