Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get last part of current URL in Laravel 5 using Blade

How to get the last part of the current URL without the / sign, dynamically?

For example:

In www.news.com/foo/bar get bar.

In www.news.com/foo/bar/fun get fun.

Where to put the function or how to implement this in the current view?

like image 401
Ronald Zwiers Avatar asked Jun 19 '16 12:06

Ronald Zwiers


People also ask

How do I find my URL in blade?

To get the current URL you can call the "url()" function and then chaining it with the "current()" function which will return the current page URL. Do note that when using this method you can get the URL path value and from the result, you can determine whether to show or hide values to the user.

How do I get Uri segment in laravel blade?

Get a Segment from Current URL in Laravel If you need to only get a segment from the current URL in Laravel, use the segment() method from the Request() class. Pass the number of the segment to get as the first argument.

How can I get current page URL in laravel?

Accessing The Current URLecho url()->current(); // Get the current URL including the query string... echo url()->full();

How do I go back to a previous URL in laravel?

Redirecting users to the previous page or url in laravel is very easy. You have to use url()->previous() method on Redirect Fasade. url()->previous() method gives you the last visited page url and Redirect Fasade return you to that page.


2 Answers

Of course there is always the Laravel way:

request()->segment(count(request()->segments())) 
like image 104
user2094178 Avatar answered Oct 29 '22 14:10

user2094178


You can use Laravel's helper function last. Like so:

last(request()->segments())

like image 23
yama Avatar answered Oct 29 '22 14:10

yama