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

Uladzimir's avatar

How to correctly add '#element' to url request

Don't solve the problem with correct showing final pages of my tests. They are need in showing immediately from the special block, not from the site header. This block named #aftertest and need place in address string.

From all that's I tried works only one solution written in app.js file

var color_data = "";
var count = 7;

var return_color_btn = $('#return_color_btn');

$('.test_color-inner').on('click', function () {
    return_color_btn.removeClass('hidden');
    color_data += $(this).attr('data-content');
    $(this).addClass('no-color');
    if (count === 0) window.location = "/test/result/" + color_data + "#aftertest";else count--;
});

Here is an example address: http://localhost/test/result/04361725#aftertest and correctly show block with id = "aftertests"

BUT it works on the first page, on the second and etc it shows like: http://localhost/test/result/04361725?page=2 and "#aftertests" don't add on end of request. And page shows from header, not from my special block.

Here is my route

Route::get('/test/result/{result}', [TestController::class, 'result']);

In TestController have sample query:

$url = url()->current();
        $data1 = substr($url, -8, 1);
        $data2 = substr($url, -7, 1);
        $comments = $film->comments()->where('active', 1)
            ->orderBy('created_at', 'desc')
            ->limit(3)
            ->get();

        switch ($data1) {
            case 0: //GREY
                switch ($data2) {
                    case 1: //GREY - BLUE
                        $array = Test::where([
                            ['b','2'],
                            ['d','1'],
                            ['g','2'],
                            ['h','1'],
                        ]);
                        $tests = $array->inRandomOrder()->paginate(1);
                        $lazyloads = $array->simplePaginate(4);
                        return view('films.testing', ['tests'=>$tests, 'lazyloads'=>$lazyloads, 'comments'=>$comments]);

after that query we go to result page, where I need in "#aftertest" on the end address string.

How to correctly fix my problem?

0 likes
3 replies
Cudesa69's avatar

This is my first time on the site - really happy to find this support group. I have personally struggled with internet addiction issues and look forward to exploring the resources here.

mvd's avatar
mvd
Best Answer
Level 48

@uladzimir tried Appending Hash Fragments ?

If you need to append a "hash fragment" to URLs generated by the paginator, you may use the fragment method. For example, to append #users to the end of each pagination link, you should invoke the fragment method like so:

$users = User::paginate(15)->fragment('aftertest');

https://laravel.com/docs/9.x/pagination#appending-hash-fragments

1 like

Please or to participate in this conversation.