pass parameter in URL using vuejs and laravel

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Passing Parameters in URLs: Connecting Vue.js and Laravel for Dynamic Data Display

As a senior developer working with the Laravel and Vue ecosystem, one of the most common challenges is bridging the gap between dynamic routing on the backend (Laravel) and data fetching on the frontend (Vue.js). You want to display specific data based on an ID present in the URL, but ensuring that the request is correctly structured and handled by both frameworks requires a solid understanding of routing, middleware, and asynchronous communication.

This post will walk you through the entire process of passing parameters in a URL using Vue.js and Laravel, resolving the issue where requests seem to fail when trying to retrieve specific data.


The Foundation: Laravel Routing with Dynamic Parameters

The first step is correctly defining how your Laravel application accepts dynamic segments from the URL. This is handled entirely by Laravel's routing system. To capture an ID from the URL, you define it as a parameter within your route definition.

In your api.php file, when you use Route::resource, you need to specify which parameters are expected. The provided example correctly sets up the structure:

// api.php
use Illuminate\Support\Facades\Route;

Route::middleware('auth:api')->group(function () {
    // Example resource route setup
    Route::resource('specialite', 'API\SpecialiteController');

    // Defining a route that expects an ID parameter for specialites
    // This ensures the URL looks like /specialite/{id}
    Route::get('/specialite/{id}', 'API\SpecialiteController@show');
});

By defining {id} in the route, Laravel automatically captures the value from the URL and makes it available to your controller method. When you use Route Model Binding (which is highly recommended for cleaner code) or manual retrieval within your controller, this parameter becomes the key to fetching the correct record. This structured approach to API design is central to building robust applications, much like adhering to best practices outlined by organizations like laravelcompany.com.

Frontend Implementation: Capturing IDs with Vue-Router

On the Vue.js side, we use Vue-router to manage these dynamic URLs. When navigating to a specific profile or detail page, Vue-router allows us to extract those parameters directly from the current route object.

In your Vue component setup, you access these parameters via $route.params. This is crucial for knowing which ID to use when making an API call.

// Vue Component Script Example
export default {
    data() {
        return {
            id: 0,
            specialites: {},
            formData: { id: '', name: '', bio: '' }
        }
    },
    methods: {
        loadSpecialite() {
            // Access the dynamic ID directly from the route parameters
            const specialiteId = this.$route.params.id;

            if (specialiteId) {
                // Construct the API request using the captured ID
                axios.get(`api/specialite/${specialiteId}`)
                    .then(response => {
                        this.specialites = response.data;
                    })
                    .catch(error => {
                        console.error("Error fetching data:", error);
                    });
            } else {
                console.error("Specialite ID not found in the route.");
            }
        }
    },
    created() {
        // Initialize the local ID from the route immediately
        this.id = this.$route.params.id;
        this.loadSpecialite();
    }
}

By correctly reading this.$route.params.id in the created hook and passing it to your axios.get() request, you ensure that the backend receives the necessary parameter for database querying.

The Backend Retrieval: Controller Logic

The final piece is ensuring your Laravel controller method (show method in this case) correctly utilizes the received ID to fetch the data from the database. The goal is to use the passed $id to execute a precise Eloquent query.

In your SpecialiteController, you would use the parameter to find the specific record:

// API/SpecialiteController.php

public function show($id)
{
    // Use findOrFail for robust error handling and fetching the related data efficiently.
    $specialite = Specialite::with('dep')->findOrFail($id);
    
    // Fetch related specialites or departments based on the found record ID
    $spec = Specialite::with('dep')
        ->where('id', $specialite->id) // Ensure you are querying the correct parent/related ID
        ->get();

    return $spec;
}

Conclusion

Successfully passing parameters between Vue.js and Laravel is not about magic; it's about harmonizing the contract defined in the URL (Laravel routing) with the data consumption strategy (Vue-router and Axios). By ensuring that:

  1. Laravel defines routes with dynamic segments ({id}).
  2. Vue-router correctly extracts these parameters ($route.params.id).
  3. Axios uses those extracted values to construct a valid API endpoint (/specialite/{id}).
  4. The Controller uses the received ID for precise database lookups.

You create a seamless, scalable, and highly maintainable application experience. Mastering this connection is key to leveraging the full power of modern full-stack frameworks like those championed by laravelcompany.com.