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?
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.
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.
Accessing The Current URLecho url()->current(); // Get the current URL including the query string... echo url()->full();
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.
Of course there is always the Laravel way:
request()->segment(count(request()->segments()))
You can use Laravel's helper function last
. Like so:
last(request()->segments())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With