slickness
slickness

Reputation: 246

How to create a route with slug or id

How can I call the user's profile with an ID or slug over the route? If the user calls his own profile it should be able to open it via slug profile / {slug} If the privacy of the visited profile is 3, it should only be accessible via the ID. profile / {id} In a different privacy, it should be able to be called only via slug.

I tried the code, but unfortunately it does not work.

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:

Route::get('/profile/{param}', 'Profile\ProfilesController@index')->name('profile');

Upvotes: 0

Views: 110

Answers (1)

Gihan Lakshan
Gihan Lakshan

Reputation: 403

$param parameter will receive user id or slug, so try below code:

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();
    }
}

User own profile and privacy level not in 3 profiles are accessible via slug, so you don't want to check is auth user accessing his own profile.

Upvotes: 2

Related Questions