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

lorvent's avatar

how to check if route is users/$id

Hello, I have menu where i need to show a menu item active. if we are currently displaying user profile

so i have Route::get('{userId?}', array('as' => 'users.show', 'uses' => 'UsersController@show'));

how can i write an if condition to check whether i am on user profile or not,

Any help is appreciated

0 likes
12 replies
bashy's avatar

All you have is an ID to show a profile? You could pass a var to the view or just check for the URI ID?

lorvent's avatar

Sorry, i don't get you but.... in my view i have

<li {{ (Request::is('admin/view_user') ? 'class="active"' : '') }}> <a href="{{ URL::to('admin/view_user').'/'.$user->id }}"> <i class="fa fa-angle-double-right"></i> View Profile </a> </li> i need to write correct code inside li so that it will be active

chrisgeary92's avatar

Sorry, That should say $user->id. Laracast forum is terrible on the iPhone. @jeffreyway please fix :) using chrome on iOS.

2 things.. Cannot edit an already posted message.. And, if I wrote a message, switch to another tab, then switch back, the screen goes white ad I cannot finish what I was writing :(

lorvent's avatar

Hi Chris, Thanks for that but i am getting a strange error

ErrorException (E_UNKNOWN) Route [1] not defined.

industriousagency's avatar

The way I've approached a profile link, is to use a designated route for it as I feel that if you're editing your profile, you'd want the URL structure to be /profile, not /users/edit/1 or similar.

You'd need to adjust the namespaces and logic etc, but you could use something like:

routes.php

//  Profile
Route::get('profile', [
  'as' => 'admin.profile',
  'uses' => 'Controllers\Admin\UsersController@getProfile'
]);

userscontroller.php

/**
  * Show the form for editing the specified resource.
  *
  * @param int $id
  * @param bool $profile
  *
  * @return Response
  */
 public function edit($id = 0, $profile = false)
 {
  $view = $profile ? 'admin.content.users.profile' : 'admin.content.users.edit';

  return View::make($view, [
   'user' => User::findOrFail($id)
  ]);
 }

 /**
  * Return the logged in user profile 
  * 
  * @return Response
  */
 public function getProfile()
 {
  return $this->edit(Auth::user()->id, TRUE);
 }

Using this method, you could then just use Request::is('profile')

chrisgeary92's avatar

Sorry, struggling on my phone here haha.. Try:

{{ is_route(['users.show', $user->id]) ? 'class="active"' : '' }}

lorvent's avatar

@industrious.mouse Thanks for the suggestion, but its for admin and he should be able to see all users so your logic may not work for me.

@chris, Thanks that worked perfectly, but i have another issue...as said above...admin should be able to see all profiles and menu should be active for any {id} Do you have idea how to do that?

Thanks.

chrisgeary92's avatar
Level 13

That won't work with the is_route() function.

You can use the * wild card with Request::is() so something like:

Request::is('user/*')

lorvent's avatar

Holy shit, how i wasted 3hours for that simple solution.

Many thanks for your smart brain ;)

and your is_route is excellent idea aswell

chrisgeary92's avatar

@lorvent - Hey.. It was bugging me that the is_route() function I wrote didn't support the * wildcard.. It's just occurred to me that it does. You can do this:

{{ is_route(['users.show', '*']) ? 'class="active"' : '' }}

Please or to participate in this conversation.