简体   繁体   中英

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.

When using route model binding (which you should always be doing) Laravel handles the lookup for you. For example:

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

No lookups needed, and Laravel will automatically handle 404s for you.

As mentioned in comments, you can also specify the key used to lookup the model instance:

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

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

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

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)

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, eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM