In Laravel, you can use the last method of the Request object to retrieve the last segment of the URL:

use Illuminate\Http\Request;

Route::get('/{any}', function (Request $request) {
    $lastSegment = $request->segment(count($request->segments()));
    return $lastSegment;
});

Alternatively, you can use the basename function to retrieve the last segment of the URL:

use Illuminate\Http\Request;

Route::get('/{any}', function (Request $request) {
    $path = $request->path();
    $lastSegment = basename($path);
    return $lastSegment;
});

Leave a Reply

Your email address will not be published. Required fields are marked *

Upgrade PHP Version without using cPanel setting