How to Get URL Segment in Laravel?

By Hardik Savani April 16, 2024 Category : Laravel

Hi Friends,

In this quick guide, we will teach you how to get url segment in laravel. I would like to show you laravel get url segment in view. Here you will learn laravel get url segment in controller. I would like to share with you get url segment in laravel blade. you will do the following things for get last url segment in laravel.

You can use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 versions.

Whenever you need to get current url segment then you can get using Request facade. I give you example one for using Request facade. so you can get segment from url and how you can see both example.

Laravel Get URL Segment in Blade File:

<!DOCTYPE html>

<html>

<head>

<title>how to get url segment in laravel - ItSolutionStuff.com</title>

</head>

<body>

{{ Request::segment(1) }}

{{ request()->segment(1) }}

</body>

</html>

Laravel Get URL Segment in Controller File:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$segment = $request->segment(1);

dd($segment);

}

}

I hope it can help you...

Tags :
Shares