How to Generate Dynamic URL in Laravel 9?

10-Apr-2023

.

Admin

How to Generate Dynamic URL in Laravel 9?

Hi Guys,

This article will provide some of the most important example of how to generate a dynamic url In laravel 9. I’m going to show you how to create a dynamic url in laravel 9. if you want to see an example of a dynamic url in laravel 9 then you are in the right place. We will use how to make a dynamic url in laravel 9.

Today, In this tutorial I will explain you to how to get generate a dynamic url in laravel 9. if you don't know how to how to generate A dynamic url in laravel 9. So don't worry I will tell you so laravel 9 provide url helper that 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 a full example for laravel 9 generate url in the controller so follow my all steps.

Download Laravel


Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

composer create-project laravel/laravel example-app

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(1);

dd(url("/product/{$product->id}"));

}

}

Output:

"http://localhost:8000/product/1"

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 these methods may also be accessed via the URL facade:

use Illuminate\Support\Facades\URL;

echo URL::current();

It will help you...

#Laravel 9