The issue you're encountering is due to a misunderstanding of how routeIs works in Laravel. The routeIs method is used to check if the current route name matches a given pattern. It does not take route parameters into account. Instead, it only checks the route name itself.
In your case, you are trying to match the route name groups.show with parameters, but routeIs is not designed to handle parameters. It simply checks if the current route's name matches the specified pattern.
If you want to check if the current route is groups.show and also verify the parameter, you will need to do this in two steps:
- Check if the route name matches.
- Check if the parameter matches.
Here's how you can achieve this:
if (request()->routeIs('groups.show') && request()->route('group')->slug === $group->slug) {
// The current route is 'groups.show' and the group slug matches
}
In this code:
request()->routeIs('groups.show')checks if the current route name isgroups.show.request()->route('group')->slugretrieves thegroupparameter from the route and compares itsslugwith$group->slug.
This approach ensures that both the route name and the parameter are correctly validated.