Passing route parameters to the route() helper in Laravel 5.4

There are a few different ways to pass these parameters. Let’s imagine a route defined as users/{userId}/comments/{commentId}. If the user ID is 1 and the comment ID is 2, let’s look at a few options we have available to us.

Option 1

route('users.comments.show', [1, 2])
// http://9url.ml/users/1/comments/2

Option 2

route('users.comments.show', ['userId' => 1, 'commentId' => 2])

// http://9url.ml/users/1/comments/2

Option 3

route('users.comments.show', ['commentId' => 2, 'userId' => 1])

// http://9url.ml/users/1/comments/2

Option 4

route('users.comments.show', ['userId' => 1, 'commentId' => 2, 'opt' => 'a'])

// http://9url.ml/users/1/comments/2?opt=a

Non-keyed array values are assigned in order; keyed array values are matched with the route parameters matching their key and anything left over is added as a query parameter.

Comments

Popular posts from this blog

Offers on Friday, April 24, 2020

Fatal: LoadModule: error loading module 'mod_sql_mysql.c'