How to Get URL Segment in 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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Call Controller Function in Blade Laravel?
- Laravel Call Function from Same Controller Example
- Laravel Ajax DELETE Request Example Tutorial
- Laravel nl2br Blade Directive Example
- Laravel - How to Check If Array is Empty in Blade?
- Laravel Ajax Request with Validation Example
- How to Create Resource Controller in Laravel?
- Laravel Create Bootstrap Contact US Form Example
- How to Check Request is Ajax or Not in Laravel?
- How to Get Current Controller Name in View Laravel?
- How to Check Request Method is GET or POST in Laravel?
- Ajax - Cross-Origin Request Blocked in Laravel?