How to generate URLs for routes with parameters in Laravel?

In Laravel, you can generate URLs for routes with parameters using the route() function or the URL::route() method. Here's how you can do it:

  1. Define a named route in your routes/web.php file or any other route file:
Route::get('/user/{id}', 'UserController@show')->name('user.show');
  1. In your controller or view, use the route() function or URL::route() method to generate the URL for the named route:

Using the route() function:

$url = route('user.show', ['id' => $userId]);

Using the URL::route() method:

use Illuminate\Support\Facades\URL; $url = URL::route('user.show', ['id' => $userId]);

In both cases, user.show is the name of the route, and the second argument is an array of parameters. The id parameter is passed as an associative array element with the corresponding value ($userId in the example).

You can then use the generated URL for redirects, links, or any other purposes in your Laravel application.