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

brentxscholl's avatar

Grabbing the #id in a url

I have a page.blade.php set up that is using bootstrap tabs and panels.

Each tab has an id. When a tab is clicked the url is updated with the id.

ie. if you click the media tab the url changes to example.com/page?#media if you click the description tab the url changes to example.com/page?#description

I have js code on this page to grab the url, check the parameters ie. #media, and open the associated tab/panel.

So if you visit example.com/page?#media the media panel will be showing. If you visit example.com.page?#description the description panel will be showing.

Is there a way in laravel that I can check the url and preform conditions based on the result?

What I want to do is:

@if($pageUrl == "#media")
do this
@elseif($pageUrl == "#description")
do that
@endif 
0 likes
3 replies
mooseh's avatar

I dont think this is something that is server sided or recognized by the serverside?

you would wanna do it like this

for the routes serversided would be like this

http://localhost/mypage?tab=media

http://localhost/mypage?tab=description

@if (request()->tab == "media")
    my media
@elseif (request()->tab == "description")
    my description
@else
    my default page!
@endif

this is definitely something that would be better handled by javascript in your case

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>

<!--body stuff -->


<script>
var hash = window.location.hash;
$('div'+hash+':first').show();

</script>
Cronix's avatar

As far as I know, the # isn't sent to the server in the request (nothing to do with laravel). It's meant for the browser to use. You can read the # with javascript though with window.location.hash.

You could also send it as another query string parameter, which could be read by laravel.

example.com/page?section=media#media
1 like
onyashed's avatar

How about through route....

@if(case1)
route('managepage',[pageno1,tabno1])
@else
route('managepage',[pageno2,tabno2])
@endif

Please or to participate in this conversation.