Get current page name from url using laravel blade

Viewed 5141

I want to get current page name from url using laravel blade, as I want to use it for dynamic manipulation and put it inside hidden value.

If the URL is admin/coding/colors, I want to get only colors page name.

Is that possible?

4 Answers

If you want it to respond to any uri, irrespective of how many segments are in the url, try:

{{substr(strrchr(url()->current(),"/"),1)}}

This will always get the last segment of the request

Of course, try this:

use \Illuminate\Support\Facades\Request;

{{Request::segment(3)}}
{str_after(url()->current(), 'http://admin/coding/')}}

You should obtain the value in the controller and give it as a variable to your view. You can do that with the segments method of the Request.

$request->segments()[count($request->segments()) -1]

Your controller should look something like this:

public function someFunctionName(Request $request)
{
    $last_url_segment = $request->segments()[count($request->segments()) -1];
    return view('view.name', ['last_url_segment', $last_url_segment]);
}

Then, in your view, you can use the variable.

<input type="hidden" name="url" value="{{ $last_url_segment }}">
Related