A Guide to Using Multiple Parameters in Laravel Routes

Last updated 12th November, 2023

As a Laravel developer, I'm always working with web applications that require complex data handling and navigation. I commonly arrive at a scenario where when defining a route, be in a web application or an API - where I need to pass two variable parameters to a Laravel Route.

In this article, we'll explore how to make the most of this powerful feature to create more flexible and dynamic web applications.

Utilising multiple parameters in Laravel routes not only makes it easier to organise and manage data, but creates a logical, well-structured route tree that can be easily expanded upon. By understanding how to pass multiple parameters in Laravel routes, we can create more efficient and maintainable applications.

Basics of Laravel Routes

In a typical Laravel application, routes are defined in the routes/web.php file, which directs a specific URL request to a designated controller method. It also provides a way to pass parameters from the URL to the controller, enabling it to carry out various operations based on these values.

The framework offers a straightforward and expressive syntax for building these routes. For instance, to create a basic GET route that returns a simple "Hello World", I can write the following code in the routes/web.php file:

use Illuminate\Support\Facades\Route;

Route::get('/greeting', function () {
    return 'Hello World';
});

More complex applications often require passing parameters through routes. In such cases, Laravel's flexibility allows me to define routes that accept multiple parameters with ease. To demonstrate, let's suppose I need a route for displaying information about a blog post, which takes a parameter: the post's slug identifier. Here is an example of such a route definition:

Route::name('posts.show')
  ->get('/posts/{slug}', 'PostController@show');

I've enclosed the parameters within curly braces and separated them by slashes in the route's URI. Laravel will automatically catch these parameters and pass them as arguments to the show method in the PostController.

Passing Multiple Parameters

I often find myself working with routes that require multiple parameters. For instance, let's reconsider our example from before. Say we need to go through a post's category to access it. We now need to pass two parameters, the category slug and the post slug.

To achieve this, I first need to define the route in the routes/web.php file. To create a route that accepts multiple parameters, I can simply add them within curly braces {}. That looks something like this:

// routes/web.php
Route::name('categories.posts.show')
    ->get('/categories/{category}/posts/{post}', 'PostController@show');

These two arguments category and post will now be passed to the show method in the PostController.

Let's say I want to generate a URL containing these parameters. In that case, I can use the route() helper function in Laravel to do so:

$url = route('example.show', [
    'category' => 'example-category',
    'post' => 'example-post'
]);

This will generate the URL /categories/example-category/posts/example-post.

Once I have the route in place, I need to handle the parameters in my controller's show method. To access them, I simply add them as arguments to the method:

class PostController extends Controller
{
	  public function show(string $category, string $post)
    {
        // $category - 'example-category'
        // $post - 'example-post'
    }
}

Now, I can use these parameters within my show method. This enables me to retrieve relevant data based on the category and post values, or even modify the application's behaviour depending upon their content.

To summarise, working with multiple parameters in Laravel routes allows me to efficiently pass data between different parts of my application. By clearly defining routes and utilising the appropriate helper functions, I can ensure a seamless experience when handling multiple data points like id and name within my Laravel application.

Implicit Scoped Bindings

An additional powerful feature Laravel offers is scoped bindings. When you make use of Laravel's model binding functionality, you can use the structure of your URL to instructure Laravel on how to retrieve related records.

...Laravel will automatically scope the query to retrieve the nested model by its parent using conventions to guess the relationship name on the parent...

Let's look at the same example, but with route binding added:

Route::name('categories.posts.show')
    ->scopeBindings() // Note the addition of `scopeBindings`
    ->get('/categories/{category}/posts/{post}', 'PostController@show');
use App\Models\{Category, Post};

class PostController extends Controller
{
    public function show(Category $category, Post $post)
    {
        //
    }
}

When you add the scopeBindings method to the route definition, you're telling Laravel to discover the Post model as a child of the Category model. Laravel will look up the category model, then search for the post model in its relationships. if it doesn't find it, it'll 404.

Optional Parameters

Laravel offers us a simple API for making our URL parameters optional in their route definitions. You simply append your parameter with a question mark? allowing the route helper to construct the URL even if that parameter is absent. Let's look at an example:

// routes/web.php
Route::name('user.show')
  ->get('/user/{team}/{status?}', [UserController::class, 'show']);

// Http/Controllers/UserController.php
class UserController extends Controller
{
	  public function show(string $team, ?string $status = null)
    {
        // ...
    }
}

The above code snippet designates a simple user controller method that allows us to fetch users related to teams in an application. We have two route parameters here. Firstly, the required team parameter, which determines which team we should fetch users from. Secondly, the optional status parameter might allow us to only fetch users in a certain status.

We can generate this URL using Laravel's route helper:

// First let's generate the route with the included
// status parameter
$url = route('user.show', [
    'team' => 'marketing',
    'status' => 'enabled'
]);

// Reults in the URL...
// /user/marketing/enabled

// Now let's generate the same route URL, but this time
// omitting the status parameter
$url = route('user.show', ['team' => 'marketing']);

// Reults in the URL...
// /user/marketing

Using optional route parameters allows us to develop further features and more complex APIs in our Laravel applications. And while the example above is a little contrived, the principle is powerful.

Adding Constraints to Parameters

Should I need to set some constraints on the parameters, Laravel allows me to do so by adding additional route definitions. For example, if I wanted to enforce that the slug parameter must be a letter or a hyphen, I could use the where method as follows:

Route::name('posts.show')
    ->get('/posts/{slug}', 'PostController@show')
    ->where('post', '[a-zA-Z-]+');

Common Mistakes and How to Fix Them

Here, I'll go through a few common "gotchas" that have caught me out a few dozen times, and tell you how to avoid the same mistakes.

1. Route Parameters do not Match the Controller Arguments

The parameters in your route definitions MUST match the arguments defined in your application controllers. If they are slightly off, it might be challenging to debug. Here's an example:

// routes/web.php
Route::name('user.show')
    ->get('/user/{userId}', [UserController::class, 'show']);

// Http/Controllers/UserController.php
class UserController extends Controller
{
	  public function show(int $id)
    {
        //
    }
}

The above sample won't work because the parameter name userId does not match the method argument id.

2. Optional Parameters Before Required Parameters

In an example where we have two route parameters, the required parameter must come first. This is because if the optional parameter is not present, the second parameter will be put on the end of an invalid URL. Let's take another user example, and assume that that user might be in a team:

// routes/web.php
Route::name('user.show')
    ->get('/user/{team?}/{username}', [UserController::class, 'show']);

// Http/Controllers/UserController.php
class UserController extends Controller
{
	  public function show(?string $team = null, string $username)
    {
        //
    }
}

In the above example, if you were to generate a URL for this route without a team parameter, you could end up with a malformed URL:

$url = route('user.show', [
    'username' => 'cbowofrivia'
]);

// The missing "team" parameter results in a malformed
// URL...

// /user//cbowofrivia

Conclusion

Utilising multiple parameters in Laravel routes is a powerful feature that can help create more flexible and dynamic web applications. By understanding the basics of Laravel routes, creating and passing parameters, adding constraints to route parameters, and passing optional parameters to routes, developers can make the most of this feature to create efficient and maintainable applications.

No comments yet…

DevInTheWild.

Login To Add Comments.

Want More Like This?

Subscribe to occasional updates.

Related Articles