Home » How To Pass Laravel URL Parameter

How To Pass Laravel URL Parameter

Last updated on May 28, 2021 by

As a beginner, you might don’t know how to pass the Laravel URL parameters like id, category, or any other. In this article, we will learn how to pass parameters in the Laravel URL. Let’s just jump into it.

Pass Laravel URL Parameter

There are basically required and optional parameters that you can pass in the URL. Let’s see both one by one with examples.

01 Required Parameters

As the name suggests, you have to pass the parameters in the URL. Let’s say we need to pass the post ID then you can define as below:

/* For laravel 8 and above */
Route::get('/posts/{id}', [PostsController::class, 'show'])
        ->name('posts.show');

/* For lower version than laravel 8*/
Route::get('/posts/{id}', 'PostsController::class@show')
        ->name('posts.show');

It is always good to use named routes instead of defining the URLs in the blade file or anywhere else because whenever you change the URLs in the routes file then you also need to change the same in relevant places.

Now, let’s add the link in blade file using named route 'posts.show' and pass the ID in the Laravel URL parameter as below:

<a href="{{ route('posts.show', ['id' => $post->id]) }}">Read More</a>

After that, you can use ID parameter in Controller as below:

<?php

namespace App\Http\Controllers;

use App\Models\Post;
use Carbon\Carbon;

class PostsController extends Controller
{
    public function show($id)
    {
        return 'Post'. $id;
    }
}

You may define as many route parameters as required by your route:

/* For laravel 8 and above */
Route::get('/posts/{post}/comments/{comment}', [PostsController::class, 'postComments'])
        ->name('posts.comments');

/* For lower version than laravel 8*/
Route::get('/posts/{post}/comments/{comment}', 'PostsController::class@postComments')
        ->name('posts.comments');

02 Pass Optional Parameters In Laravel URL

Sometimes, we may need to pass the parameter which is not required every time, in this case, we can pass the optional parameter. To do that, you need to add the ? mark after the parameter’s name. Also, you need to add the route variable default value.

/* For laravel 8 and above */
Route::get('/posts/{id?}', [PostsController::class, 'show'])
        ->name('posts.show');
<?php

namespace App\Http\Controllers;

use App\Models\Post;
use Carbon\Carbon;

class PostsController extends Controller
{
    public function show($id=null)
    {
        if ($id):
            $post = Post:find($id);
        else:
            $post = Post:all();
        endif;

        return view('posts.index', [
            'post' => $post
        ]);

    }
}

In this above example, we have added default value $id=null. Then we are checking if id is not null then fetch the particular post else fetch all the post.

So if you access URL like, http://example.local/posts then it will fetch all the posts and if you access a URL like, http://example.local/posts/2 then it will only fetch the post with ID 2 Thus we can use an optional parameter to use a single route and do multiple things.

Additionally, read our guide:

  1. How to Select Data Between Two Dates in MySQL
  2. Error After php artisan config:cache In Laravel
  3. Specified Key Was Too Long Error In Laravel
  4. AJAX PHP Post Request With Example
  5. How To Use The Laravel Soft Delete
  6. How To Add Laravel Next Prev Pagination
  7. cURL error 60: SSL certificate problem: unable to get local issuer certificate
  8. Difference Between Factory And Seeders In Laravel
  9. Laravel: Increase Quantity If Product Already Exists In Cart
  10. How To Calculate Age From Birthdate
  11. How To Check Laravel PHP Version
  12. How To Handle Failed Jobs In Laravel
  13. How To Remove WooCommerce Data After Uninstall
  14. How To Get Latest Records In Laravel
  15. How To Break Nested Loops In PHP Or Laravel

That’s it from our end. We hope this article helped you to understand how to pass the Laravel URL parameter.

Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you for reading this post 🙂 Keep Smiling! Happy Coding!

 
 

Leave a Comment

*

code