Snippets

Retrieve URL segment with Laravel Request

Posted by I. B. Gd Pramana A. Putra, 28 Jun 22, last updated 17 Jul 22

Laravel has a request()->segment(x) helper to let us get a part or a segment of a url.

As an example: let say we have the following url https://id.kodekativ.co/db/s/posting. Parts or segments from the URL is counted from 1, 2, and so on..

So if we want to receive db segment from the url, we can do the following:

<?php

request()->segment(1);

Or if we want to receive s instead, we only need to change the segmentation parameter like the following:

<?php

request()->segment(2);

I love sharing code snippets as most of the time, a quick code example is what we're looking for instead of long-written articles. If you think my code snippets are helpful and save you a lot of time, please consider buying me a cup of coffee :)

Support me via · paypal · buymeacoffee · ko-fi · trakteer
Contributed Snippets
Answer & Responses
    No comments yet

Wanna write a response?

You have to login before write a comment to this post.