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

Q8Xbox's avatar

direct to another page with hash in blade

Hello,

I have a welcome page which have some menu to navigate through specific areas in the main page using hashtags like #about-sec but if I go to different page like https://example.com/encyclopedia and rom there the menu items contain hashtags won't work because it will point to current page not the main page like https://example.com/encyclopedia#about-sec so I would like the check the url within blade to see if current url is https://example.com/encyclopedia than direct me to main page route with hashtag to be like https://example.com/#about-sec. I used the code below but it won't work so Is there another way to make it work properly ?

<a class="nav-link scroll link" href="{{ url()->current() == route('encyclopedia') ? redirect()->route('welcome')->withFragment('#about-sec') : '#about-sec' }}">
0 likes
12 replies
Snapey's avatar

why are your links not just routes to the homepage with the fragment added?

rooskie's avatar

@Q8Xbox You have the line of code already: route('welcome')->withFragment('#about-sec') . You don't need to check whether your own the encyclopedia page or welcome page, the browser can do that for you. If you're on the encyclopedia page, it will navigate to the home page and then find the fragment. If you're already on the home page, it will just scroll to the fragment.

<a class="nav-link scroll link" href="{{ route('welcome')->withFragment('#about-sec') }}">
poletaew's avatar

@rooskie it doesn't work (for Laravel 10 at least) because function route returns string. See source in vendor\laravel\framework\src\Illuminate\Foundation\helpers.php:

    /**
     * Generate the URL to a named route.
     *
     * @param  string  $name
     * @param  mixed  $parameters
     * @param  bool  $absolute
     * @return string
     */
    function route($name, $parameters = [], $absolute = true)
    {
        return app('url')->route($name, $parameters, $absolute);
    }

And, because of that, we get an error "Call to a member function withFragment() on string"

Q8Xbox's avatar

@thinkverse It will not work since after click url will be like this: about:blank#blocked

<li class="nav-item"><a class="nav-link scroll link" href="{{ route('welcome') }} . #about-sec"><span data-count="2" class="num-nav">2</span><span>0</span><br>الوصف</a></li>
Snapey's avatar

@Q8Xbox about:blank is not a valid URL so what are you talking about?

Look at the source in your browser and check the href for the item.

You don't need to use php concatenation in html

href="{{ route('welcome') }}#about-sec">

or as per @thinkverse answer, inside the curly braces

href="{{ route('welcome') . '#about-sec' }}">   
Snapey's avatar

@Q8Xbox

Look at the source in your browser and check the href for the item.

Q8Xbox's avatar

@Snapey if simply click on the menu it will do nothing but if open it in new tab works fine ?

Please or to participate in this conversation.