Laravel – Optional Route Parameters

Hey,

In this small post, I will show you an example of how we can use optional route parameters in Laravel PHP framework.

Let’s assume that in your view file [‘book.create.blade.php’] you have the following code:

Here we wrote just two routes, what can we do when there are a lot of other routes that can be handled by a single method, without creating a new method for each new category book?

Well, as the title of this post, in Laravel you can add optional parameters to the route.
So you can replace the above routes with something like:

So now in your book’s view file [‘book.create.blade.php’] you can call the routes in this way:

Notice how we didn’t pass any parameters to the last route of creating a fiction book.

Just in case you didn’t notice the routes above is how it’s can be written in Laravel 5.1 version, in later version the route’s name can be written in another similar way:

There are a lot of useful tricks and shortcuts that you can use, the Laravel documentation in their official website has a perfect information that you can grasp in no time.

Thanks for reading the post.
I wish you the best.

mhmd

Hello, I'm Mohammad Daka, software engineer who love coding. In my spare time I blogs about JavaScript,PHP, Chrome Extensions, helpful resources and tools, I'm also working on my own projects. Don't hesitate to contact me at my twitter account: @mohammad_daka :)

You may also like...

1 Response

  1. backupbot says:

    Well, as the title of this post, in Laravel you can add optional parameters to the route.So you can replace the above routes with something like.website backup

Leave a Reply to backupbot Cancel reply

Your email address will not be published. Required fields are marked *