Search code examples
laravellaravel-routing

Laravel route /users/id or /users/username to return users.show


I would like to be able to accept either a 40-character string ID /users/{id} or the user's username /users/{username} and then return the users.show view.

  1. Is this possible?
  2. Where would the checks go?

Solution

  • I have found another solution to this. I don't know which is better though. Hopefully the community can vote...

    Within my UsersController.php I have:

    public function show($id_or_username)
        {
            $user = User::where('id' , '=', $id_or_username)->orWhere('username', $id_or_username)->firstOrFail();
            return View::make('users.show', compact('user'));
        }