toyop
toyop

Reputation: 119

Laravel: How to use a parameter other than user id in a URL

I'm creating a Laravel web site where users can login and upload images. For visitors to see that user's gallery, I've set up a gallery page with a URL like https://localhost8000/gallery/2, where 2 is the user ID.

My current code looks like this.

web.php

Route::get('/menu/{user}', 'CartController@menu');

CartController.php

public function menu($user)
{
    $user = User::findOrFail($user);
    return view('new_menu')->with(['user' => $user]);
}

Instead of using the user ID as a route parameter, I want to use a random string. Currently a random string of 32 digits is created when the user registers and stored in the users table in a column called random.

I couldn't figure out how to relate "random" to "user" in web.php and Controller.

Upvotes: 1

Views: 1637

Answers (4)

miken32
miken32

Reputation: 42696

When using route model binding (which you should always be doing) Laravel handles the lookup for you. This behaviour is triggered by including the parameter type in the method signature:

public function menu(User $user)
{
    return view('new_menu')->with(['user' => $user]);
}

No lookups needed, and Laravel will automatically handle 404s for you. (Note the name of the route parameter must also match the name of the variable in the method signature.)

You can also specify the key used to lookup the model instance, instead of the default of using the model's primary key:

Route::get('/menu/{user:random}', 'CartController@menu');

This presumes that the random column has a UNIQUE index, otherwise results may be unpredictable.

Upvotes: 3

Adewale Charles
Adewale Charles

Reputation: 55

You will have to do a route model binding, you can do something like this in the model you want the URL to have other data, e.g if you want the username instead of id, in the user model you will add this line of code.

/**
     * Get the route key for the model.
     *
     * @return string
     */
    public function getRouteKeyName()
    {
        return 'username';
    }

Note whatever value you are returning must be an existing value in the user table.

Upvotes: 0

Maik Lowrey
Maik Lowrey

Reputation: 17556

First, add in your user migration file the field:

'user_gallery_id' => Illuminate\Support\Str::random(32);

Then in your route for https://localhost8000/gallery/2:

Route::get('/gallery/{user_gallery_id}', function($user_gallery_id) {
  return User::where('user_gallery_id', $user_gallery_id)->with('gallery')->get();
});

I don't know your Controller and Models. You can edit and customise it. But that is roughly the worklfow for this usecase. I just did it with a closure now. You can edit and customise it. But that is roughly the worklfow for this usecase. I just did it with a closure now. I assume that Gallery and User are already in relation to each other.

In your blade file you have the access to the gallery:@dd($user->gallery)

Upvotes: 0

Mah Es
Mah Es

Reputation: 630

ok first, something is wrong with your route. you said you want users to visit "gallery/2" for user id=2 gallery of images right? but instead of 2, you want to use a 32 char string to find the user. just do this:

Route::get('/gallery/{userString}', 'CartController@gallery');


public function gallery($userString)
{
    $user = User::where('random',$userString)->first();
    if($user == null)
      // return some error about user not found

    return view('new_menu')->with(['user' => $user]);
}

always rememberm it does not really matter what you pass in the route as a parameter, it could be any number or string. its about what you want to do with it in the controller. just like the code above which we search for a user who has a matching random column with the $userString

Upvotes: 0

Related Questions