{"id":202,"date":"2021-12-28T19:21:25","date_gmt":"2021-12-28T19:21:25","guid":{"rendered":"https:\/\/mouryasolutions.in\/blog\/?p=202"},"modified":"2023-01-30T19:45:05","modified_gmt":"2023-01-30T19:45:05","slug":"get-last-segment-from-url-in-laravel","status":"publish","type":"post","link":"https:\/\/mouryasolutions.in\/blog\/get-last-segment-from-url-in-laravel\/","title":{"rendered":"Get last segment from URL in Laravel"},"content":{"rendered":"<div class=\"post-content\">\r\n<blockquote class=\"wp-block-quote\">\r\n<p>In Laravel, you can use the <code>last<\/code> method of the <code>Request<\/code> object to retrieve the last segment of the URL:<\/p>\r\n<\/blockquote>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>use Illuminate\\Http\\Request;\r\n\r\nRoute::get('\/{any}', function (Request $request) {\r\n    $lastSegment = $request-&gt;segment(count($request-&gt;segments()));\r\n    return $lastSegment;\r\n});\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p>Alternatively, you can use the <code>basename<\/code> function to retrieve the last segment of the URL:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>use Illuminate\\Http\\Request;\r\n\r\nRoute::get('\/{any}', function (Request $request) {\r\n    $path = $request-&gt;path();\r\n    $lastSegment = basename($path);\r\n    return $lastSegment;\r\n});\r\n<\/code><\/pre>\r\n<\/div>","protected":false},"excerpt":{"rendered":"<p>In Laravel, you can use the last method of the<a href=\"https:\/\/mouryasolutions.in\/blog\/get-last-segment-from-url-in-laravel\/\">Read More<i class=\"fa fa-long-arrow-right\" aria-hidden=\"true\"><\/i><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/posts\/202"}],"collection":[{"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/comments?post=202"}],"version-history":[{"count":6,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/posts\/202\/revisions"}],"predecessor-version":[{"id":542,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/posts\/202\/revisions\/542"}],"wp:attachment":[{"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/media?parent=202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/categories?post=202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/tags?post=202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}