ItSolutionStuff.com

How to Get URL Segment in Laravel?

By Hardik Savani • April 16, 2024
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: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Call Controller Function in Blade Laravel?

Read Now →

Laravel Call Function from Same Controller Example

Read Now →

Laravel Ajax DELETE Request Example Tutorial

Read Now →

Laravel nl2br Blade Directive Example

Read Now →

Laravel - How to Check If Array is Empty in Blade?

Read Now →

Laravel Ajax Request with Validation Example

Read Now →

How to Create Resource Controller in Laravel?

Read Now →

Laravel Create Bootstrap Contact US Form Example

Read Now →

How to Check Request is Ajax or Not in Laravel?

Read Now →

How to Get Current Controller Name in View Laravel?

Read Now →

How to Check Request Method is GET or POST in Laravel?

Read Now →

Ajax - Cross-Origin Request Blocked in Laravel?

Read Now →