Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

eloperdev's avatar

Passing parameters through a request object (Vue/Axios/Laravel)

I must start off by saying that I am new to Laravel and Axios, and Vue (descending order of newness)....but have been working on a few projects for a few months now.

I think this is a simple question, but I haven't been able to find the answer anywhere. Please set me straight if you can. :-)

I have started a new project using a very recent version of Homestead, with Laravel 5.4. I'm using Vue, and Axios as the http library, to communicate with a Laravel back-end (not really using blades, vue is my front-end for this single page app).

My problem is that I would like to be able to pass a request object and read the parameters in the Laravel controller, without specifying all the parameters as part of the route. I've seen this done on another project I worked on, but it was an earlier version of Laravel. For me, I can't seem to get any parameters on the other side this way, so I'm wondering if its still possible, or if I've done something wrong.

I would like to be able to do something like this:

axios.get('api/v1/dailydata', {
    location: this.location,
    startDate: this.startDate,
    endDate: this.endDate
}).then (response => { 
    // handle success
}

and have my route defined as:

Route::prefix('/api/v1/')->group(function () {
    Route::get('dailydata/}', 'DailyDataController@index');
    ...
}

and in my controller I'd like to use the Request object and do something like this:

class DailyEntryController extends Controller
{
    public function index(Request $request) {
        // want to see all input, it prints nothing
        Log::info( "request is " . print_r($request->all(), true ) );
        // want to see location input, it prints nothing
        Log::info( "request is " . print_r($request->input('location'), true ) );

        // validation of $request should starts
...

However, this isn't working, though I've tried a few variations on it.

I have been able to get my parameters through if I do the following (using a hardcoded example for the url, though I do have it working with a generated url):

// create url string that includes parameters
axios.get('/api/v1/dailydata/Amsterdam/2014-01-01/2014-02-01')

and change my route to

    Route::get('dailydata/{location}/{startDate}/{endDate}', 'DailyEntryController@index');

and the controller function prototype to

public function index($location, $startDate, $endDate) {

To me it seems nicer if I could use the method at the top, without having to build up the url string and have all the parameters explicitly defined in the route. Now, perhaps this is not the "right" way to think about it (to me it seems nicer but I'm still new to Laravel) - but I guess I'm interested to know if it should be possible, or am I missing something?

Thanks in advance for any tips.

0 likes
5 replies
Cronix's avatar
Cronix
Best Answer
Level 67

Well, GET requests are done entirely via the URL (it's getting something, not sending something. Well except for the url...). I think you're wanting a POST request to SEND the data through to the controller. It should work the way you want if you change to a POST route, and then use POST in axios.

Route::post('dailydata', 'DailyDataController@index');

axios.post('api/v1/dailydata', {
    location: this.location,
    startDate: this.startDate,
    endDate: this.endDate
})
3 likes
eloperdev's avatar

Thanks for the response, Cronix. I didn't know that GET requests are expected to be done entirely via the URL.

From what I understand on reading about REST APIs (new to this as well), what I'm doing fits the billing of a GET request. The POST seems tempting for convenience though, especially if there would be even more params!

Cronix's avatar

Another option with get is to use the querystring for passing parameters.

Route::get('dailydata', 'DailyDataController@index');

api/v1/dailydata?location=amsterdam&startdate=2014-01-01&enddate=2014-02-01

echo $request->location;  //amsterdam
echo $request->startdate; //2014-01-01
echo $request->enddate; //2014-02-01
2 likes
jackson51002's avatar

@CRONIX - Thank you.This was the final clue I needed to get dependent values based on dropdown selections.

Please or to participate in this conversation.