How To Generate Dynamic URL In Laravel?


Share:
Published By: Admin, Published On: Jul 03, 2021, Category: Laravel 8 Laravel

Hi Guys,

Today, In this tutorial I will explain you to how to get generate dynamic url in laravel 8.if you don't know how to how to generate dynamic url in laravel.So don't worry i will tell you so laravel provide url helper may be used to generate arbitrary URLs for your application. The generated URL will automatically use the scheme (HTTP or HTTPS) and host from the current request.

Here, I will give you full example for laravel generate url in controller so follow my all steps.

Solution
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Product;

class TestController extends Controller
{
    /**
     * Write Your Code..
     *
     * @return string
    */
    public function index()
    {
        $product = Product::find(2);
        dd(url("/product/{$product->id}"));
    }
}

Output
"http://localhost:8000/product/2"    
Get Current URL:

If no path is provided to the url helper, a Illuminate\Routing\UrlGenerator instance is returned, sanctioning you to access information about the current URL:

// Get the current URL without the query string...
echo url()->current();

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

// Get the full URL for the previous request...
echo url()->previous();

So,you can generate of these methods may also be accessed via the URLfacade:

use Illuminate\Support\Facades\URL;

echo URL::current();

It will help you...

Share: