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

Rubenfppinto's avatar

Customize the web route to accept username instead of ID

Hi guys,

I have a project where each user has their own individual page that displays products, but the profiles are public in which users can share URL's with each other. For that, I am trying to get the URL for the profiles to user their username (which I made unique on registration) rather than id, but I am having a bit of trouble.

This is my routes files. the first one is the one I am trying to customize with the username.

Route::get('/index/{user:username}', 'ProductsController@index')->name('products.index');

Route::get('/product/create', 'ProductsController@create');
Route::post('/product', 'ProductsController@store');
Route::get('/product/{product}/edit', 'ProductsController@edit')->name('product.edit');
Route::patch('/product/{product}', 'ProductsController@update')->name('product.update');
Route::delete('/product/{product}', 'ProductsController@destroy')->name('product.destroy');

Then the index method in my ProductsController is:

public function index(User $user)
    {
        $products = auth()->user()->products;
        $username = User::where([
            'username' => $user->username,
        ])->first();

        return view('index', compact('products'));
    }

The reason I have /index/ in the route is because I would like it to have a generic URL rather that /user or /product (not sure if this is possible).

My apologies is the code is messy but I am a beginner.

0 likes
15 replies
jlrdw's avatar

The jetstream / fortify docs explain using username instead of email.

Sounds like you need a guest setup.

Make the username a slug is one option, but this sounds insecure what you are wanting to do.

Make it like an online shop.

Rubenfppinto's avatar

I thought having the id in the URL would be less secure that using the username?

I also thought of having a field in my table where a one time unique random string would be generated at registration and would use that as the identifier in the url.

Sorry, what do you mean "make it like an online shop"?

jlrdw's avatar

Sorry, what do you mean "make it like an online shop"?

Where you can look around "shop" first prior to having to login. Like "Window Shopping".

Rubenfppinto's avatar

oh no, my application is a wishlist. So each user have their page with products

Snapey's avatar

What is not working?

Also, you already have $user, no need to query the User model again?

I assume you want to see the products of the user that is mentioned in the URL?

public function index(User $user)
{
	$products = $user->products;

        return view('index', compact('products'));
}

Do you have products as a User relationship?

What Laravel version?

1 like
Rubenfppinto's avatar

Right now, with my code the application after login, looks at the authenticated user and redirects to the index page and displays only his own products. However, index is a generic route and I would like a custom routes that users can share between them in order to see each others products. Kinda like facebook and twitter do if you share your own profile. You will have foo.com/username.

  • I am using laravel 7
  • Yes, User can have many products and a product belongs to a user.
Snapey's avatar

so whats wrong?

when you enter in the url /index/nameofuser what happens

Rubenfppinto's avatar

I just get a page display 404 error but the username does exist

Rubenfppinto's avatar

Yeah, every product route is working ok. The index route where I just display all the products for the authenticated user is working ok (maybe not the best convention, but I am using index display all products for one user, since for my application I don't need a generic index to display all products for all users). I have logged in with 2 different users and indeed they only see their own stuff.

Snapey's avatar

Your routes does not show a route for the user to see their own products? Do you have other routes that you have not shown?

Rubenfppinto's avatar

It is shown in the index route

Route::get('/index/{user:username}', 'ProductsController@index')->name('products.index');

Then in the index actions of the ProductsController, I fetch the authenticated user's products, pass them to the index view, loop over them and display only the ones for that user

Rubenfppinto's avatar

I did look at that previously but I wasn't sure if I had to return the key I want in the getRouteKeyName()

Please or to participate in this conversation.