pietrovismara
pietrovismara

Reputation: 6291

Append a hash to url in Laravel using pagination

My route:

Route::get('/app/search', ['as' => 'search', 'uses' => 'AppController@getSearch']);

My controller:

public function getSearch()
{        
    $searchTerm = Input::get('s');
    $locs = Loc::whereRaw('match(title, description) against(? in boolean mode)', [$searchTerm])
        ->paginate(10); 
    $locs->getFactory()->setViewName('pagination::slider');        
    $locs->appends(['s' => $searchTerm]);
    $this->layout->with('title', 'Search: ' . $searchTerm);
    $this->layout->main = View::make('home')
        ->nest('content', 'index', ($locs->isEmpty()) ? ['notFound' => true] : compact('locs'));
}

The url i get is: "www.example.com/app/search?s=search+term"

I'd like to get: "www.example.com/app/search?s=search+term#result" to go directly to the result element.

I tried to use $locs->fragment('result')->links();both in controller and view, but nothing changed.

People are asking for more informations like to show where the url is generated, but the problem is i don't know it. Neither the Route, the Controller and the View seems responsible for that.

What's weird in this is that i can't understand how the url is generated, i thought that:

$locs->appends(['s' => $searchTerm]);

is responsible for that, but even if i delete that line nothing changes in the url. So i'm really stuck on that, laravel docs is not of help and i can't find docs or help on the web.

Upvotes: 4

Views: 2770

Answers (3)

Shanewyz Molla
Shanewyz Molla

Reputation: 21

As you per Laravel Documentation You are using $locs->appends(['s' => $searchTerm]); is okay.

You now just to add one more thing before to apply appends $locs = Loc::whereRaw('match(title, description) against(? in boolean mode)', [$searchTerm])->paginate(10)->fragment('result'); Adding ->fragment('result') will help you provide the correct solution.

Upvotes: 2

Samy Massoud
Samy Massoud

Reputation: 4385

For now Laravel supports this feature

As per Laravel documentation you need to use fragment method

{{ $users->fragment('result')->links() }}

Upvotes: 4

fico7489
fico7489

Reputation: 8560

I created workaround with jquery :

@if($orders->hasPages())
    <div class="section">
        <div class="section__content hash-pagination" data-hash="promo-orders">
            {!! $orders->appends(request()->all()) !!}
        </div>
    </div>
@endif


<script>
    $(document).ready(function () {
        $('.hash-pagination').each(function(element){
            var hash = $(this).data('hash');
            var links = $(this).find('a');

            links.each(function(link){
                var href = $(this).attr('href');
                $(this).attr('href', href + '#' + hash);
            });
        });
    });
</script>

Upvotes: 0

Related Questions