axios get passing parameter to laravel route

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Parameter Passing: Getting Axios Data to Laravel Routes Correctly

As developers working with modern stacks like Vue.js and Laravel, we often encounter situations where data needs to flow seamlessly between the frontend request layer (Axios) and the backend routing layer (Laravel). The specific issue you're facing—receiving a 404 error when using query parameters instead of route parameters—is extremely common. It highlights a fundamental difference in how URLs are structured versus how route definitions are mapped in frameworks like Laravel.

This post will diagnose why your setup failed and provide the correct, robust methods for passing dynamic data from your Vue application to your Laravel API endpoints.


The Root of the 404 Error: Route vs. Query Parameters

The problem lies in a mismatch between what your route is expecting and what your Axios request is sending.

Your Setup Analysis:

  1. Laravel Route Definition:

    Route::get('bpaper/{id}', function($id) { /* ... */ });
    

    This definition tells Laravel that the route expects a dynamic segment directly in the URL path, like /bpaper/12. It expects the parameter to be part of the path.

  2. Axios Request (What you sent):

    axios.get('http://localhost/laravel_back/public/api/bpaper',{
      params: { id: 12 } // This creates a query string: ?id=12
    });
    

    Axios, when using the params object, correctly constructs a URL with query parameters: /api/bpaper?id=12.

The Conflict: Laravel receives the request for /api/bpaper?id=12, but its route is defined to look only for /api/bpaper/12. Since the path segment {id} is missing in the actual request URL, the router cannot match the request to any defined route pattern, resulting in a 404 Not Found error.


Solution 1: The Recommended Approach – Using Route Parameters (Path Segments)

For fetching specific resources identified by an ID, the RESTful and most robust way in Laravel is to use Route Parameters. This keeps your URLs clean, descriptive, and aligns perfectly with how complex routing systems manage resources.

Step 1: Update the Laravel Route

Modify your route definition to correctly expect the dynamic segment in the URL path:

// routes/api.php

// Change this: Route::get('bpaper/{id}', ...)
Route::get('bpaper/{id}', function ($id) {
    // Access the ID directly from the route parameters
    return 'Paper ' . $id;
});

Step 2: Update the Axios Request

Now, your frontend request must construct the full path, including the parameter directly in the URL string, rather than relying on query parameters.

// Vue/Axios Code
const idToSend = 12;
axios.get(`http://localhost/laravel_back/public/api/bpaper/${idToSend}`)
  .then(response => {
    console.log(response.data); // Will successfully return 'Paper 12'
  })
  .catch(error => {
    console.error('Error fetching data:', error);
  });

This approach is superior because it clearly defines the resource being requested (e.g., "Get the paper with ID 12") and aligns perfectly with Laravel’s routing conventions, which is a key principle in building scalable APIs, as discussed on platforms like laravelcompany.com.


Solution 2: When to Use Query Parameters (Filtering/Optional Data)

If the parameter you are sending is not an essential identifier for the resource itself—but rather optional filtering, sorting, or pagination data—then using Query Parameters (?key=value) is the correct method.

If you needed to fetch all papers where id was perhaps a filter (though usually this would be handled by separate filtering routes), your original Axios approach would work perfectly:

// This works if your Laravel route is defined to accept query parameters:
axios.get('http://localhost/laravel_back/public/api/bpaper', {
  params: {
    id: 12 // Creates: /api/bpaper?id=12
  }
})
// Laravel Controller must be set up to read these parameters:
// public function index(Request $request) { ... }

Conclusion

The key takeaway is understanding the distinction between Path Parameters (used for identifying resources, e.g., /users/{id}) and Query Parameters (used for filtering or optional data, e.g., ?sort=asc).

For fetching a specific resource by its unique ID, always favor defining that ID as part of the URL path in your route definition and passing it directly via Axios. This ensures clean routing, predictable API design, and better maintainability for any application built on the Laravel framework.