代码之家  ›  专栏  ›  技术社区  ›  slickness

如何使用slug或id创建路由

  •  0
  • slickness  · 技术社区  · 6 年前

    如何通过路由调用带有ID或slug的用户配置文件? 在另一个隐私中,它应该只能通过slug调用。

    我试过这个代码,但不幸的是它不起作用。

    public function index($param)
    {
        $user = User::firstOrFail();
    
        if(Auth::user() === $user){
            $user = User::where('slug', $param)->first();
        }elseif ($user->profile->privacy === 3){
            $user = User::where('id', $param)->first();
        }else{
            $user = User::where('slug', $param)->first();
            }
    }
    

    路线:

    Route::get('/profile/{param}', 'Profile\ProfilesController@index')->name('profile');
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Gihan Lakshan    6 年前

    $param 参数将接收用户id或slug,请尝试以下代码:

    public function index($param)
    {
        $user = User::where('slug', $param)->first();
    
        if($user)
        {
            if($user->profile->privacy === 3)
            {
                // This user profile cannot access via slug
            }
            // User Accessible via slug
        }
        else
        {
            $user = User::where('id', $param)->first();
        }
    }