DEV Community

Morcos Gad
Morcos Gad

Posted on • Edited on

Route Parameter Validation Where in Route - Laravel

Let's start quickly here. We want to check who is coming from a route with a specific parameter to fetch the data

Route::get('users\{period}',[IndexController::class, 'index']);
Enter fullscreen mode Exit fullscreen mode

so we used in_array()

public function index($period){
  if(!in_array($period, ['week','month'])) {
     abort(404);
  }
  //...
}
Enter fullscreen mode Exit fullscreen mode

Let's see the best way is to put where in Route and dispense in_array()

Route::get('users\{period}',[IndexController::class, 'index'])
     ->where('period','week|month');
Enter fullscreen mode Exit fullscreen mode

Source :- https://www.youtube.com/watch?v=TPfyHe7h-h4
I hope you enjoyed the code.

Top comments (0)