Passing a variable through url in laravel
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
As a new Laravel developer, you may have encountered scenarios where you want to provide specific parameters or variables through your application's URL. In this blog post, we'll discuss how to do just that with proper route configuration and using the Laravel framework's features like controllers and views.
Firstly, let us analyze your sample code provided in the question. It shows a Route in the Route.php file, where a GET request is mapped to the 'blog/{category}' URI, which accepts a category parameter. The route then processes the query and displays data based on the passed category. Meanwhile, your BlogController handles the functionality of your application by fetching posts from the database. Let's dive into how these can be combined to pass variables through URLs.
To achieve this, you can create a custom route in Route.php for the 'blog/{category}' URI, which will map the category parameter as follows:
Route::get('blog/{category}', function($category = null) {
if ($category) {
$posts = Post::where('category', '=', $category)->get();
} else {
$posts = Post::all();
}
// ...
});
Here, we define a new route that accepts the category parameter and uses it to fetch posts associated with that specific category. Note that you're using an optional parameter ($category = null), which means it will accept requests even without it. If no category is passed in the URL, the default behavior will be to show all blog posts.
Next, let us modify your BlogController as follows:
class BlogController extends BaseController {
public function index() {
return view('blog.index', [
'posts' => Post::all(),
]);
}
}
This code snippet utilizes the Laravel's Eloquent ORM to fetch all posts from the database and pass it to the view. Since we already have a view named 'blog.index', it will be used by default when calling this controller method.
To put it together, you can use HTML to create links for each post category in your blog index view:
@foreach ($posts as $post)
<h2>{{ $post->id }}</h2>
<p>{{ $post->name }}</p>
<p>{{ $post->category }}</p>
<h2>{{ HTML::link(
action('BlogController@index', array($post->category)),
$post->category)
}}
@endforeach
The link tags within your blade file are using the 'action' helper function to generate URLs that correspond to the specific route defined earlier in your Route.php file. When a category is clicked, it will redirect users to the blog index page with the appropriate category parameter passed through the URL.
In conclusion, passing variables through URLs within Laravel applications can be achieved by defining a custom route and controller that handles the logic for displaying data based on the requested parameters. Remember to use best practices, such as Eloquent ORM for database interactions, and well-structured code organization to ensure a clean and efficient application.